0

我有一个包含sqlite记录的表,我想将记录的值分配给一个变量,我尝试过这种方式:

latitudeB = database.execSQL("SELECT latitude FROM " + TABLE_NAME + "WHERE _id in (SELECT COUNT(_id)-1 FROM " + TABLE_NAME + ")");

但 execsql 返回无效。sql代码似乎在sqlite中工作

4

1 回答 1

0

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
}
于 2013-03-24T08:36:00.253 回答