2013-07-06 116 views
3

我不能在我的Android應用程序破解SQLite中一個簡單的更新查詢..這裏是查詢,更新查詢源碼 - Android電子

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) 

回答

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" 
2
  1. UPDATE語句總是隻有一個表,所以在表名前添加表名是沒有意義的,也是不允許的。
  2. 在SQL中,字符串使用'分隔。 "用於列名稱等標識符;當您嘗試處理word這個詞時,您的查詢就會爆炸。
  3. 爲了避免字符串格式化的問題,您應該使用參數:
sqliteDB.rawQuery("UPDATE ... WHERE word = ?", 
         new String[] { word_name });