SqlCommand.ExecuteScalar Method
Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
I guess this will involve heavy use of generics.
Assume I have an SQLiteDatabase / Cursor object.
Have a look at SQLLiteStatement
long simpleQueryForLong()
Execute a statement that returns a 1 by 1 table with a numeric value.
String simpleQueryForString()
Execute a statement that returns a 1 by 1 table with a text value.
This logic worked for me:
public int ExecuteScalar(/* code...*/) {
Cursor cursor = database.rawQuery(sql, selectionArgs);
try {
cursor.moveToNext();
int val = cursor.getInt(0);
cursor.close();
return val;
}
catch (Exception e) {
/* code...*/
}
return -1;
}
给数据库对象是db
long result = db.compileStatement("select count(*) from some_table").simpleQueryForLong();
如果要检索主键,则可以使用语句getGeneratedKeys方法,该方法在插入后返回生成键的resultSet。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.