Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select query in sqlite android

Tags:

android

sqlite

String temp_address="nothing";
    try
    {
        String selectQuery = "SELECT lastchapter FROM Bookdetails INTO"+temp_address+"WHERE bookpath=?";
        db.execSQL(selectQuery, new String[] { fileName });
        System.out.println(temp_address+" result of select Query");
    }

    catch(Exception e)
    {
        System.out.println(e+" is the error here");

    }
    finally
    {
        db.close();
    }

Logcat

android.database.sqlite.SQLiteException: near "bookpath": syntax error: , while compiling: SELECT lastchapter FROM Bookdetails INTOnothingWHERE bookpath=?

i just want to take the result of the above query so that the string stored in lastchapter is available in temp_address please help

i am new to android sqlite database please help

like image 459
Mukund Avatar asked Nov 28 '22 21:11

Mukund


2 Answers

There are SQL syntax problems and you'll need to use a Cursor to retrieve query results, for example with rawQuery():

String selectQuery = "SELECT lastchapter FROM Bookdetails WHERE bookpath=?";
Cursor c = db.rawQuery(selectQuery, new String[] { fileName });
if (c.moveToFirst()) {
    temp_address = c.getString(c.getColumnIndex("lastchapter"));
}
c.close();
like image 127
laalto Avatar answered Dec 13 '22 22:12

laalto


The logcat said it all, you've forgot spaces. To get data into the string:

String temp_address="nothing";
String[] args = new String[] { fileName };
Cursor cursor = sqLiteDatabase.rawQuery("SELECT lastchapter FROM Bookdetails WHERE bookpath=?", args);
if (cursor.moveToFirst()){
    temp_address = cursor.getString(cursor.getColumnIndex("lastchapter"));
}
cursor.close();
like image 42
nikis Avatar answered Dec 13 '22 23:12

nikis