Select query in sqlite android

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 request so that the string stored in lastchapter is accessible in temp_address please help

I am new to android sqlite database please help

+4
source share
3 answers

There are syntax problems with SQL, and you need to use it Cursorto get the results of a query, 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();
+20
source

Correct your request below: add a space in WHEREReason

String selectQuery = "SELECT lastchapter FROM Bookdetails WHERE bookpath=? ";

Update: go with rawQuery(), return Cursorwith the results

 String selectQuery = "SELECT lastchapter FROM Bookdetails WHERE bookpath=? ";
 Cursor c = db.rawQuery(selectQuery, new String[] { fileName });
 if (c.moveToFirst()) {
 temp_address = c.getString(0);
 }
  c.close();

: http://www.higherpass.com/android/tutorials/accessing-data-with-android-cursors/

+3

Logard said everything, you forgot the spaces. To get the data in a row:

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();
+3
source

Source: https://habr.com/ru/post/1529983/


All Articles