Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio flagging error in SQLite pragma command, <pragma value> expected, got 'ON'

I think this is a new error since switching to Android Studio 3.0. I am receiving a lint syntax error:

<pragma value> expected, got 'ON'

I get this error only on the first of the following two methods:

private static void setForeignKeyConstraintsEnabled(@NonNull SQLiteDatabase db) {
    if (!db.isReadOnly()) {
        db.execSQL("PRAGMA foreign_keys=ON;");
    }
}

private static void setForeignKeyConstraintsDisabled(@NonNull SQLiteDatabase db) {
    if (!db.isReadOnly()) {
        db.execSQL("PRAGMA foreign_keys=OFF;");
    }
}

screenshot of editor text with underlined error

I only found one suggested fix on the internet as of writing. It is in german and says that I shouldn't be using constant strings for pragma but should use a string parameter.

String query = String.format ("PRAGMA foreign_keys = %s","ON"); 
db.execSQL(query); 

But I suspect if this removes the error, it only does so by making the code too complicated for the lint rule to detect.

Why would ON throw an error and not OFF? Is my syntax wrong?

like image 274
Jon Avatar asked Nov 01 '17 14:11

Jon


1 Answers

Using the signed integer alternative syntax worked.

private static void setForeignKeyConstraintsEnabled(@NonNull SQLiteDatabase db) {
    if (!db.isReadOnly()) {
        db.execSQL("PRAGMA foreign_keys=1;");
    }
}

private static void setForeignKeyConstraintsDisabled(@NonNull SQLiteDatabase db) {
    if (!db.isReadOnly()) {
        db.execSQL("PRAGMA foreign_keys=0;");
    }
}

Interestingly, using the alternative syntax of yes/no flagged an error on no but not yes. I agree with @CommonsWare that this seems to be a lint bug.

like image 109
Jon Avatar answered Nov 08 '22 09:11

Jon