我有一个包含sqlite记录的表,我想将记录的值分配给一个变量,我尝试过这种方式:
latitudeB = database.execSQL("SELECT latitude FROM " + TABLE_NAME + "WHERE _id in (SELECT COUNT(_id)-1 FROM " + TABLE_NAME + ")");
但 execsql 返回无效。sql代码似乎在sqlite中工作
Your problem is execSQL()
that is designated for statements which are not SELECT
So exactly from reference.
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
instead it you need to use Cursor
.
String query = SELECT latitude FROM " + TABLE_NAME
+ " WHERE _id in (SELECT COUNT(_id)-1 FROM " + TABLE_NAME + ")";
Cursor c = db.rawQuery(query, null);
if (c.moveToFirst()) { // if cursor is not empty
// do your work
double latitude = c.getDouble(0); // getting latitude's value
}