我正在尝试在 android studio 中使用 Sqlite,并且我想使用以下代码使用 ID 号搜索“目标”对象。
private final static String DATABASE_NAME = "Destination.db";
private final static int DATABASE_VERSION = 1;
//table name
private final static String TABLE_NAME = "Destination";
public final static String DESTINATION_ID = "destination_id";
public final static String DESTINATION_NAME = "destination_name";
public final static String DESTINATION_PRIORITY = "destination_priority";
public final static String DESTINATION_LONGITUDE = "destination_longitude";
public final static String DESTINATION_LATITUDE = "destination_latitude";
名字在上面,这是我的代码:
public Destination getDestination(int id){
SQLiteDatabase db=this.getReadableDatabase();
String selectQuery="SELECT "+
DESTINATION_ID + "," +
DESTINATION_NAME + "," +
DESTINATION_PRIORITY + "," +
DESTINATION_LONGITUDE + "," +
DESTINATION_LATITUDE + "," +
" FROM " + TABLE_NAME
+ " WHERE " +
DESTINATION_ID + "=?";
Destination destination = new Destination();
Cursor cursor = db.rawQuery(selectQuery, new String[]{String.valueOf(id)});
if(cursor.moveToFirst()){
do{
destination.setId(cursor.getInt(cursor.getColumnIndex(DESTINATION_ID)));
destination.setName(cursor.getString(cursor.getColumnIndex(DESTINATION_NAME)));
destination.setPriority(cursor.getInt(cursor.getColumnIndex(DESTINATION_PRIORITY)));
destination.setLongitude(cursor.getDouble(cursor.getColumnIndex(DESTINATION_LONGITUDE)));
destination.setLongitude(cursor.getDouble(cursor.getColumnIndex(DESTINATION_LATITUDE)));
} while(cursor.moveToNext());
}
cursor.close();
db.close();
return destination;
}
select查询似乎有问题:
Cursor cursor = db.rawQuery(selectQuery, new String[]{String.valueOf(id)});
为什么 ??我是sqlite的初学者,请帮助我!!谢谢你!
您应该从 selectQuery 中删除 last 。
String selectQuery="SELECT "+
DESTINATION_ID + "," +
DESTINATION_NAME + "," +
DESTINATION_PRIORITY + "," +
DESTINATION_LONGITUDE + "," +
DESTINATION_LATITUDE +
" FROM " + TABLE_NAME
+ " WHERE " +
DESTINATION_ID + "=?";
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句