2016-12-25 34 views
0

有沒有辦法將文本追加到我的sqlite數據庫中已有的文本上,而無需再次手動鍵入舊的代碼?將文本追加到已存在的sqlite條目

我像它會是這樣的:

UPDATE myTable set myText = myText || 'new text' 

但是,當我嘗試在Android的它返回一個 「空bindArgs」 錯誤

方法:

public void appendConvo(String name, String convo) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.execSQL("update friends set convo = convo || '" + convo + "' where name = '" + name + "'" , null); 
    db.close(); 
} 

錯誤:

FATAL EXCEPTION: main 
    Process: com.acbk.acbk, PID: 13438 
    java.lang.IllegalStateException: Could not execute method for android:onClick 
     at android.view.View$DeclaredOnClickListener.onClick(View.java:4745) 
     at android.view.View.performClick(View.java:5702) 
     at android.view.View$PerformClick.run(View.java:22546) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:158) 
     at android.app.ActivityThread.main(ActivityThread.java:7224) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
    Caused by: java.lang.reflect.InvocationTargetException 
     at java.lang.reflect.Method.invoke(Native Method) 
     at android.view.View$DeclaredOnClickListener.onClick(View.java:4740) 
     at android.view.View.performClick(View.java:5702)  
     at android.view.View$PerformClick.run(View.java:22546)  
     at android.os.Handler.handleCallback(Handler.java:739)  
     at android.os.Handler.dispatchMessage(Handler.java:95)  
     at android.os.Looper.loop(Looper.java:158)  
     at android.app.ActivityThread.main(ActivityThread.java:7224)  
     at java.lang.reflect.Method.invoke(Native Method)  
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)  
    Caused by: java.lang.IllegalArgumentException: Empty bindArgs 
     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1791) 
     at com.acbk.acbk.DataBaser.appendConvo(DataBaser.java:81) 
     at com.acbk.acbk.Chat.send(Chat.java:137) 
     at java.lang.reflect.Method.invoke(Native Method)  
     at android.view.View$DeclaredOnClickListener.onClick(View.java:4740)  
     at android.view.View.performClick(View.java:5702)  
     at android.view.View$PerformClick.run(View.java:22546)  
     at android.os.Handler.handleCallback(Handler.java:739)  
     at android.os.Handler.dispatchMessage(Handler.java:95)  
     at android.os.Looper.loop(Looper.java:158)  
     at android.app.ActivityThread.main(ActivityThread.java:7224)  
     at java.lang.reflect.Method.invoke(Native Method)  
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)  

回答

0

您正在使用execSQL(String, Object[])在其官方文檔here說:

void execSQL (String sql, Object[] bindArgs)
Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE

使用execSQL代替:

public void appendConvo(String name, String convo) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.execSQL("update friends set convo = convo || '" + convo + "' where name = '" + name + "'"); 
    db.close(); 
} 
0

SQL字符串用引號分隔;如果你的字符串包含引號,你必須正確地轉義它們。

爲了避免格式化問題就是這樣,用參數來代替:

db.execSQL("UPDATE friends SET convo = convo || ? WHERE name = ?", 
      new Object[]{ convo, name }); 
相關問題