0

I'm trying to get data from database. When I click on item in listview it should get me all data for clicked item. but I'm getting this error and cannot resolve it.

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.matija.ams/com.example.matija.ams.DisplayLogs}: android.database.sqlite.SQLiteException: near "SELECT": syntax error (code 1): , while compiling: SELECT * FROM Logs WHERE id IN SELECT id FROM logs_recepits WHERE id =?
Caused by: android.database.sqlite.SQLiteException: near "SELECT": syntax error (code 1): , while compiling: SELECT * FROM Logs WHERE id IN SELECT id FROM logs_recepits WHERE id =?

public ArrayList<Logs> getAllLogs(long id) {
        ArrayList<Logs> logList = new ArrayList<Logs>();
        String selectQuery = "SELECT * FROM " + TABLE_LOGS + " WHERE " + KEY_ID  + " IN " + ( "SELECT " + KEY_ID + " FROM " + TABLE_LOGS_BELONGS_TO_RECEPIT + " WHERE " + RECEPIT_ID + " =?");

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, new String[]{String.valueOf(id)});

        //going throug all rows and adding it to list
        if (cursor.moveToFirst()) {
            do {
                Logs log = new Logs();
                log.setId(cursor.getLong(0));
                log.setCreatedAt(cursor.getString(1));
                log.setPlate_number(cursor.getString(2));
                log.setSort_id(cursor.getString(3));
                log.setGrade(cursor.getString(4));
                log.setDiameter(cursor.getString(5));
                log.setLength(cursor.getString(6));
                logList.add(log);
            }while (cursor.moveToNext());
        }

        return logList;
    }

Question: How should I resolve this error?

2 Answers 2

1

Looks like misplaced brackets

Try this

String selectQuery = "SELECT * FROM " + TABLE_LOGS + " WHERE " + KEY_ID  + " IN " + "(SELECT " + KEY_ID + " FROM " + TABLE_LOGS_BELONGS_TO_RECEPIT + " WHERE " + RECEPIT_ID + " =?)";
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Glad I could help.
1

Try This: Brackets Should be inside in the ""

String selectQuery = "SELECT * FROM " + TABLE_LOGS + " WHERE " + KEY_ID  + " IN " +  "(SELECT " + KEY_ID + " FROM " + TABLE_LOGS_BELONGS_TO_RECEPIT + " WHERE " + RECEPIT_ID + " =?)";

2 Comments

Thanks, that was the problem. @Rohit5k2 has answered first so I'll accept his answer but I'll upvote yours.
NP happy to help you. Happy Coding :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.