3

我无法在我的 android 应用程序中破解 SQLite 中的简单更新查询。这是查询,

int fav = 1;
Cursor c = sqliteDB.rawQuery("UPDATE "+ MyConstants.TABLE_NAME + " SET "+MyConstants.TABLE_NAME+"."+MyConstants.ISFAV+ " = "+fav+ " WHERE " +MyConstants.TABLE_NAME+"."+MyConstants.WORD_NAME+ " = \""+word_name+"\"", null);

这是例外,

07-06 23:41:48.723: E/AndroidRuntime(1102): FATAL EXCEPTION: main
07-06 23:41:48.723: E/AndroidRuntime(1102): android.database.sqlite.SQLiteException: near ".": syntax error (code 1): , while compiling: UPDATE words SET words.isfavor = 1 WHERE words.word = "hello"
07-06 23:41:48.723: E/AndroidRuntime(1102):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-06 23:41:48.723: E/AndroidRuntime(1102):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1013)
07-06 23:41:48.723: E/AndroidRuntime(1102):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:624)
07-06 23:41:48.723: E/AndroidRuntime(1102):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-06 23:41:48.723: E/AndroidRuntime(1102):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
07-06 23:41:48.723: E/AndroidRuntime(1102):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
07-06 23:41:48.723: E/AndroidRuntime(1102):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
4

2 回答 2

5

您一直不恰当地使用表名。它应该更像这样:

int fav = 1;
Cursor c = sqliteDB.rawQuery("UPDATE "+ MyConstants.TABLE_NAME + " SET "+ MyConstants.ISFAV + " = "+fav+ " WHERE " + MyConstants.WORD_NAME + " = \""+word_name+"\"", null);

这应该会给你一个看起来更像这样的结果查询:

UPDATE words SET isfavor = 1 WHERE word = "hello"
于 2013-07-06T19:34:11.827 回答
2
  1. UPDATE语句总是只有一个表,因此在列名前加上表名是没有意义的,也是不允许的。
  2. 在 SQL 中,字符串用 . 分隔'"用于列名等标识符;当您尝试处理这个词时,您的查询会爆炸word
  3. 为避免字符串格式问题,您应该使用参数:
    sqliteDB.rawQuery("UPDATE ... WHERE word = ?",
                      new String[] { word_name });
于 2013-07-06T19:33:00.773 回答