SQLite pragma命令中的Android Studio标记错误,<pragma value> expected,'ON'

Jon*_*Jon 10 sqlite android android-studio android-studio-3.0

我认为这是自切换到Android Studio 3.0以来的一个新错误.我收到一个lint语法错误:

<pragma value> expected, got 'ON'

我只在以下两种方法中的第一种方法中得到此错误:

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;");
    }
}
Run Code Online (Sandbox Code Playgroud)

带有下划线错误的编辑器文本的屏幕截图

在写作时我只在互联网上找到了一个建议的修复程序.它是德语,并说我不应该为pragma使用常量字符串,但应该使用字符串参数.

String query = String.format ("PRAGMA foreign_keys = %s","ON"); 
db.execSQL(query); 
Run Code Online (Sandbox Code Playgroud)

但我怀疑如果这样可以消除错误,它只会使代码过于复杂而无法检测到lint规则.

为什么ON会抛出错误而不是OFF?我的语法错了吗?

Jon*_*Jon 6

使用带符号的整数替代语法.

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;");
    }
}
Run Code Online (Sandbox Code Playgroud)

有趣的是,使用是的替代语法/没有对标志的错误没有,但不能肯定.我同意@CommonsWare,这似乎是一个lint bug.

  • FWIW,我提交了[错误报告](https://issuetracker.google.com/issues/68755451)."ON","on"和"no"被标记为无效,后两者被明确记录为正确. (3认同)