此方法在databasehelperclass中返回预期的单个值
public Cursor getUserDataFromDb(){
SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
Cursor rawdata=sqLiteDatabase.rawQuery("SELECT NAME FROM " +TABLE_NAME+" ORDER BY ID DESC LIMIT 1)",null);
return rawdata;
}
从Activity中的usertable中获取值
try {
Cursor cursor = mydb.getUserDataFromDb();
if (cursor.moveToFirst()){
name_resultfromdb = cursor.getString(0);
}
else {
Toast.makeText(this,"Setup your Account",Toast.LENGTH_LONG).show();
}
}catch (Exception e){
e.printStackTrace();
}
我使用上面的代码来获取数据,但是我得到了null 我想将这个值显示在TextView中,但我无法得到它。
答案 0 :(得分:1)
您有语法错误(额外")"):
"SELECT NAME FROM " +TABLE_NAME+" ORDER BY ID DESC LIMIT 1)"
将其更改为
"SELECT NAME FROM " + TABLE_NAME + " ORDER BY ID DESC LIMIT 1"
答案 1 :(得分:1)
有一个helper function来读取单个值:
String result = DatebaseUtils.stringForQuery(db,
"SELECT NAME FROM "+TABLE_NAME+" ORDER BY ID DESC LIMIT 1", null);
无论如何,如果此查询未返回值,则表为空,或者最后一条记录的Name
列中的值为空。