本文介绍了SQLite pragma命令中的Android Studio标记错误,< pragma value>预期,“开启"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

我只找到一个建议在撰写本文时在Internet上进行修复.它用德语说,我不应该对常量使用常量字符串,而应该使用字符串参数.

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

但是我怀疑这是否可以消除错误,这只能通过使代码过于复杂而无法检测到lint规则来实现.

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

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

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

推荐答案

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

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

有趣的是,使用yes/no替代语法在 no 上标记了错误,但在 yes 上没有标记.我同意@CommonsWare,这似乎是一个不起毛的错误.

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.

这篇关于SQLite pragma命令中的Android Studio标记错误,< pragma value>预期,“开启"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:26