zoukankan      html  css  js  c++  java
  • Android 学习 查询数据库

    对数据库操作时,查询数据库会经常用到。在Android中查询数据库用到两个重要的类:
    SQLiteDatabase:用来创建,删除,执行SQL命令,并执行其他常见的数据库管理任务。
    query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) 返回Cursor对象 table:数据库里边表的名称
    columns:需要查询出来数据库列数组 selection:数据库查询条件,相当于where后面的条件。如果没有则用null代替
    selectionArgs:数据库where条件后面经常会带?号,这个就是?号的替代者。如果没有则用null代替
    groupBy:查询出来的数据是否需要分组。如果没有则用null代替 having:聚合操作。如果没有则用null代替
    orderBy:查询出来的数据是否需要排序。如果没有则用null代替
    Cursor:查询数据库返回的结果对象。
    isFirst():返回游标是否指向第一行。 isLast():返回游标是否指向最后一行。
    moveToFirst():移动光标到第一行。 moveToLast():将光标移动到最后一行。
    moveToNext():将光标移动到下一行。 moveToPosition(int position):将光标移动到一个绝对位置。
    moveToPrevious():移动光标到上一行。 getColumnIndex(String columnName):获得列索引,序号以0开始 getColumnIndexOrThrow(String columnName):返回给定列名称的从零开始的索引
    获得数据连接对象:

    Java代码 复制代码 收藏代码
    1. DatabaseHelper database = new DatabaseHelper(this 
    2.                     .getApplicationContext());  
    3.             Log.i("haiyang:createdb=", "执行失败");  
    4.             SQLiteDatabase db = null;  
    5.             try 
    6.             {  
    7.                 db = database.getWritableDatabase();  
    8.             }  
    9.             catch (SQLiteException ex)  
    10.             {  
    11.                 db = database.getReadableDatabase();  
    12.             }  
    13.  
    14.  
    15.             this.query(db); 
    DatabaseHelper database = new DatabaseHelper(this
    					.getApplicationContext());
    			Log.i("haiyang:createdb=", "执行失败");
    			SQLiteDatabase db = null;
    			try
    			{
    				db = database.getWritableDatabase();
    			}
    			catch (SQLiteException ex)
    			{
    				db = database.getReadableDatabase();
    			}
    
    
    			this.query(db);
    

    具体操作数据库方法:

    Java代码 复制代码 收藏代码
    1. privatevoid query(SQLiteDatabase db)  
    2. {  
    3.     Cursor cursor = db.query(TABLE_NAME, new String[]{TITLE,BODY}, null, null, null, null, null);  
    4.  
    5.          //根据自己程序需要对数据库做对应的操作  
    6.     cursor.moveToLast();  
    7.     while(!cursor.isFirst())  
    8.     {  
    9.         String title = cursor.getString(cursor.getColumnIndexOrThrow(TITLE));  
    10.         Log.i("title = ", title);  
    11.         Log.i("cursor.getCount() = ", String.valueOf(cursor.getCount()));  
    12.         cursor.moveToPrevious();  
    13.     }  
    private void query(SQLiteDatabase db)
    {
    	Cursor cursor = db.query(TABLE_NAME, new String[]{TITLE,BODY}, null, null, null, null, null);
    
             //根据自己程序需要对数据库做对应的操作
    	cursor.moveToLast();
    	while(!cursor.isFirst())
    	{
    		String title = cursor.getString(cursor.getColumnIndexOrThrow(TITLE));
    		Log.i("title = ", title);
    		Log.i("cursor.getCount() = ", String.valueOf(cursor.getCount()));
    		cursor.moveToPrevious();
    	}
    }
    
  • 相关阅读:
    python使用 db.select 返回的数据只能遍历一次
    redis学习主从配置
    php操作EXCLE(通过phpExcle实现读excel数据)
    php操作EXCLE(通过phpExcle实现向excel写数据)
    Redis 源码安装
    MongoDb环境安装
    php下载
    漂亮的jQuery tab选项卡插件
    jquery禁用右键、文本选择功能、复制按键的实现
    jquery中邮箱地址 URL网站地址正则验证实例
  • 原文地址:https://www.cnblogs.com/LiaoHao/p/3267894.html
Copyright © 2011-2022 走看看