zomry1 zomry1 - 3 months ago 7
Android Question

Get information from SQLite (Android Studio)

I am new in Android Studio and I just started to work with the SQLite
I created a SQLite file (db) and I created a function that put some data in the table:

public String FirstInsert()
{
String meassage = new String();

SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();

cv.put(COL_2,"hi how are you?");
cv.put(COL_3,"blabla");
cv.put(COL_4,"blabla");
cv.put(COL_5,"blabla");
cv.put(COL_6,"blabla");
cv.put(COL_7,"blabla");
cv.put(COL_8,"blabla");
cv.put(COL_9,"blabla");
cv.put(COL_10,"blabla");
cv.put(COL_11,"blabla");


long result = db.insert(TABLE_NAME,null,cv);
if(result == -1)
{
meassage = new String("Bad");
}
else
{
meassage = new String("Good");
}
return meassage;

}


I want the do more spesific select that take only one item, the COL_2 and the row is number that I get so I wrote this code:

public Cursor GetQuestions(int num)
{
//num++;
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT QUESTION FROM " + TABLE_NAME + "ORDER BY " + num + "ASC LIMIT 1",null);
return res;
}


this is the right code? and if it is why I got an error here too?

Thank you very much for trying to help!

Answer

yes,that's because not having space between words of sqlite code and it's better to close your cursor and your db after every query like this

res.close();
db.close();
Comments