SQLite(特别是Android) - 是否在“有效”列名中?

时间:2013-09-18 02:28:40

标签: android sqlite

我花了10分钟检查拼写等等,但我找不到错误!

所以我想知道SQLite中的in是一个有效的列名吗?我通常使用MariaDB,并正确地提供了一个转义/使用语句,通常都很好。

09-18 03:15:27.892: E/SQLiteLog(30043): (1) near "in": syntax error
09-18 03:15:27.892: E/SQLiteDatabase(30043): Error inserting state=1 time=1378810407
 reading=13964 in=0
09-18 03:15:27.892: E/SQLiteDatabase(30043): android.database.sqlite.SQLiteException:
 near "in": syntax error (code 1): , while compiling: INSERT INTO 
 Readings(state,time,reading,in) VALUES (?,?,?,?)

有帮助吗?

-- Describe READINGS
CREATE TABLE "Readings" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "reading" INTEGER,
    "in" INTEGER,
    "time" INTEGER,
    "state" INTEGER
)

使用:

ContentValues values = new ContentValues();
values.put("reading", reading.asInt());
values.put("in", in.asInt());
values.put("state",State.OKAY.ordinal());
values.put("time", time.getTimestamp());

return (int) db.insert("Readings", null, values);

in实际上是0;我有一大堆错误,有些错误值in。没有类型错误或沉默的异常。

我没想到会有任何错误,我甚至不确定是什么错误。

3 个答案:

答案 0 :(得分:2)

IN是一个关键字。如果你引用它应该有用:

INSERT INTO 
  Readings(state,time,reading,"in") VALUES (?,?,?,?)

中期,尝试更改列名。

答案 1 :(得分:1)

in实际上是一个SQL关键字,如:

select xyzzy from plugh where grue in ('twisty','passages')

因此,如果你想使用它,你可能需要转义它,例如:

select xyzzy from plugh where `in` in ('yes','maybe')

如何转换为Android方法我不确定但如果使用更合适的列名,则不会出现这些问题。轻推,轻推,眨眼,眨眼。

对不起,经过反思,这可能太迟钝了。我的意思是:in并不是一个非常具有描述性的列名。取决于它实际应该表示的内容(以及我无法立即告诉它的事实证明它不够具有描述性),我想将其更改为checkedInTimeisInLibrary

答案 2 :(得分:0)

是“in”是一个关键字,要么SQLite不让你完全绕过SQL,要么Android的包装器太薄了,它不适合你。无论修复很简单:

    ContentValues values = new ContentValues();
    values.put("reading", reading.asInt());
    values.put("\"in\"", in.asInt());
    values.put("state",State.OKAY.ordinal());
    values.put("time", time.getTimestamp());

    return (int) db.insert("Readings", null, values);

是的,只需将引号放在那里(我理解的是SQLite方式,但它确实接受`(反引号))

谢谢你们。