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

三世轮回 提交于 2019-12-05 20:08:19

问题


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;");
    }
}

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?


回答1:


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.



来源:https://stackoverflow.com/questions/47057487/android-studio-flagging-error-in-sqlite-pragma-command-pragma-value-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!