Search code examples
javasqliteandroid-activityandroid-sqlite

My app closes when I pass from main activity to another


The error :

E/SQLiteLog: (1) unrecognized token: "4LIMIT" in "SELECT * FROM t_comanda WHERE id = 4LIMIT 1"
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.storder20, PID: 8462
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.storder20/com.example.storder20.VeureActivity}: android.database.sqlite.SQLiteException: unrecognized token: "4LIMIT" (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM t_comanda WHERE id = 4LIMIT 1
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

-I have a main activity with an arrayList where you can add diferent orders, my problem is when Iwant to click on to see this order the app close. I think that the error comes from The code from the SQLite database that it is this:

 public Comanda veureComanda (int id){
        DbHelper dbHelper = new DbHelper(context);
        SQLiteDatabase db = dbHelper.getWritableDatabase();

    Comanda comanda = null;
    Cursor cursorComanda = null;

    cursorComanda = db.rawQuery("SELECT * FROM " + TABLE_COMANDA + " WHERE id = " + id + "LIMIT 1", null);

    if (cursorComanda.moveToFirst()){
        comanda = new Comanda();
        comanda.setId(cursorComanda.getInt(0));
        comanda.setNom(cursorComanda.getString(1));
        comanda.setComanda(cursorComanda.getString(2));
        comanda.setUbicació(cursorComanda.getString(3));
    }

    cursorComanda.close();

    return comanda;
}

Solution

  • there is no gap between '4' and 'LIMIT' keyword the correct request:

    SELECT * FROM t_comanda WHERE id = 4 LIMIT 1