Search code examples
sqliteandroid-cursorloader

SQLite cursor query error "like" and "==" not working on content provider "selection" definition


this may be a silly error on my SQLite "selector" definition, but can't seem to figure it our from similar questions and errors posted so far. I am likely making a silly mistake, but can't for the life of me find it. Please let me know if you need more info to be able to help!!

So this works perfectly well:

selection = "((" +
                TransContract.TransEntry.COLUMN_DATE + " > " +
                Long.toString(dateOutput) + ") AND (" +
                TransContract.TransEntry.COLUMN_DATE + " <= " + Long.toString(dateOutputEnd) + "))";

But when I try to query the COLUMN_CATEGORY (which is TEXT format), in this way:

selection = TransContract.TransEntry.COLUMN_CATEGORY + " like " + '%'+
                "Transport General" + "%";

OR in this way:

selection = TransContract.TransEntry.COLUMN_CATEGORY + " == " +
                "Transport General";

I get a "E/AndroidRuntime: FATAL EXCEPTION"

My cursor query:

cursor = database.query(
                    TransContract.TransEntry.TABLE_NAME,
                    projection,
                    selection,
                    selectionArgs,
                    null,
                    null,
                    sortOrder);

My DbHelper (where the CATEGORY column is found):

String SQL_CREATE_ENTRIES =
            "CREATE TABLE " + TransContract.TransEntry.TABLE_NAME + " (" +
                    TransContract.TransEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    TransContract.TransEntry.COLUMN_AMOUNT + " REAL, " +
                    TransContract.TransEntry.COLUMN_TYPE + " INTEGER NOT NULL DEFAULT 0, " + // debit, credit
                    TransContract.TransEntry.COLUMN_ACCOUNT + " INTEGER, " + // wallet, savings, more later.
                    TransContract.TransEntry.COLUMN_ACCOUNT_FROM + " INTEGER, " + // defaults from wallet 0
                    TransContract.TransEntry.COLUMN_ACCOUNT_TO + " INTEGER, " + // defaults back to wallet 0
                    TransContract.TransEntry.COLUMN_CATEGORY + " TEXT, " +
                    TransContract.TransEntry.COLUMN_TITLE + " TEXT, " +
                    TransContract.TransEntry.COLUMN_DATE + " INTEGER, " +
                    TransContract.TransEntry.COLUMN_RECURRING + " INTEGER NOT NULL DEFAULT 0," +
                    TransContract.TransEntry.COLUMN_FREQUENCY + " TEXT," +
                    TransContract.TransEntry.COLUMN_END_DATE + " INTEGER" +
                    ");";

EDIT

Thank you for your comments, I should have been more specific! My stack trace is indeed a syntax error, and it is coming from the new line of code which I am pasting.

Caused by: android.database.sqlite.SQLiteException: near "General": syntax error (code 1): , while compiling: SELECT _id, category, title, amount, type, date FROM transactions WHERE category == Transport General ORDER BY date DESC 

The problem was the single quotes inside the double quotes for the String (thanks @Ton Plooij) but strangely the compare values did not need the single quotes.

Here is the working solution:

selection = TransContract.TransEntry.COLUMN_CATEGORY + " = " +
                "'Transport General'";  

Solution

  • I don't know how you programming language works but both your failing SQlite query strings seem to be created incorrectly. The like query should look like "TransContract.TransEntry.COLUMN_CATEGORY like '%Transport General%'". You construct the comparison string without a opening and closing single quote inside the query. The second query has two problems: the '==' should be a single '=' and again the compare value is not enclosed in single quotes.