Reputation: 95
This method is in databasehelperclass returning single value expected
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;
}
fetching value from usertable inside an Activity
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();
}
I am using the above code to get the data, but I am getting null.
I want to show this value into a TextView, but I am unable to get it.
Upvotes: 3
Views: 5178
Reputation: 180290
There is a helper function to read a single value:
String result = DatebaseUtils.stringForQuery(db,
"SELECT NAME FROM "+TABLE_NAME+" ORDER BY ID DESC LIMIT 1", null);
Anyway, if this query does not return a value, then the table is empty, or the value in the Name
column of the last record is empty.
Upvotes: 3
Reputation: 69396
You have a syntax error (extra ")"):
"SELECT NAME FROM " +TABLE_NAME+" ORDER BY ID DESC LIMIT 1)"
change it to
"SELECT NAME FROM " + TABLE_NAME + " ORDER BY ID DESC LIMIT 1"
Upvotes: 0