Ⅰ android 数据库条数查询方法
DBHelper 继承SQLiteopenHelper
DBHelper helper=new DBHelper(......);
SQLitedatebase db=helper.getReadabledatabase();
db.exe("select count as totalcount from tablename");
Ⅱ android sqlite数据库怎样写带条件的查询语句
c = db.rawQuery("select _id,ration,album_id,size,album_pic,artist_pic,title,data,album,artist,recentiy_time from musictbl where recentiy_time <> 0",null);
排序可以对list进行排序,在music类里实现一下排序的接口就可以了吧
Ⅲ Android 中数据库查询方法query() 中的selectionArgs 参数只能在编译之前确定,这怎么实现动态查询
网上找来的
Android 中涉及数据库查询的地方一般都会有一个 query() 方法,而这些 query 中有大都(全部?)会有一个参数 selectionArgs,比如下面这个 android.database.sqlite.SQLiteDatabase.query():
view plain to clipboardprint?
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
selection 参数很好理解,就是 SQL 语句中 WHERE 后面的部分,即过滤条件, 比如可以为 id=3 AND name='Kevin Yuan' 表示只返回满足 id 为 3 且 name 为 "Kevin Yuan" 的记录。
再实际项目中像上面那样简单的“静态”的 selection 并不多见,更多的情况下要在运行时动态生成这个字符串,比如
view plain to clipboardprint?
public doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" id " AND name='" name "'", // selection
//...... 更多参数省略
);
}
public doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" id " AND name='" name "'", // selection
//...... 更多参数省略
);
}
在这种情况下就要考虑一个字符转义的问题,比如如果在上面代码中传进来的 name 参数的内容里面有单引号('),就会引发一个 "SQLiteException syntax error .... "。
手工处理转义的话,也不麻烦,就是 String.replace() 调用而已。但是 Android SDK 为我们准备了 selectionArgs 来专门处理这种问题:
view plain to clipboardprint?
public void doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" id " AND name=?", // selection
new String[] {name}, //selectionArgs
//...... 更多参数省略
);
// ...... 更多代码
}
public void doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" id " AND name=?", // selection
new String[] {name}, //selectionArgs
//...... 更多参数省略
);
// ...... 更多代码
}
也就是说我们在 selection 中需要嵌入字符串的地方用 ? 代替,然后在 selectionArgs 中依次提供各个用于替换的值就可以了。在 query() 执行时会对 selectionArgs 中的字符串正确转义并替换到对应的 ? 处以构成完整的 selection 字符串。 有点像 String.format()。
不过需要注意的是 ? 并不是“万金油”,只能用在原本应该是字符串出现的地方。比如下面的用法是错误的:
view plain to clipboardprint?
public void doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"? = " id " AND name=?", // selection XXXX 错误!? 不能用来替换字段名
new String[]{"id", name}, //selectionArgs
//...... 更多参数省略
);
// ...... 更多代码
}
Ⅳ Android SQLite 数据库查询问题,紧急!~在线等~~
表结构是这样的:String TABLE_NAME = "OrderEntryInfo"; //table name String ORDER_ID = "OrderId"; String TABLE_NO = "TableNo"; String ITEM_ID = "ItemId"; String ITEM_ORDER_TIMES = "ItemOrderTimes"; String STAFF_ID = "StaffId"; String CUSTOMER_NUMBER = "CustomerNumber"; String CUSTOMER_AGE_LEVEL = "CustomerAgeLevel"; String ORDER_TIME = "OrderTime"; String ORDER_TYPE = "OrderType"; String ORDER_PAY_STATUS = "OrderPayStatus"; String SQL_CREATE_ORDERENTRYINFO = "CREATE TABLE " + TABLE_NAME + "(" + ORDER_ID + " INTEGER PRIMARY KEY, " + TABLE_NO + " INTEGER NOT NULL , " + ITEM_ID + " INTEGER NOT NULL , " + ITEM_ORDER_TIMES + " INTEGER NOT NULL , " + STAFF_ID + " TEXT NOT NULL, " + CUSTOMER_NUMBER + " TEXT NOT NULL, " + CUSTOMER_AGE_LEVEL + " TEXT NOT NULL, " + ORDER_TIME + " TEXT NOT NULL, " + ORDER_TYPE + " TEXT, " + ORDER_PAY_STATUS + " TEXT)";