Android Studio 在 SQLite pragma 命令中标记错误,<pragma value>预期,得到“开" [英] Android Studio flagging error in SQLite pragma command, <pragma value> expected, got 'ON'

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

问题描述

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

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

我只找到了一个 在撰写本文时建议在互联网上修复.它是用德语写的,并说我不应该将常量字符串用于编译指示,而应该使用字符串参数.

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?

推荐答案

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

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

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

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆