将文本追加到已存在的sqlite条目中

时间:2016-12-25 02:04:17

标签: android sqlite

有没有办法将文本附加到我的sqlite数据库中的文本上,而无需再次手动输入旧代码?

我想象它会是这样的:

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

但是当我在android中尝试它时会返回“Empty 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) 

2 个答案:

答案 0 :(得分:0)

您正在使用官方文档here所说的execSQL(String, Object[])

  

void execSQL(String sql,Object [] bindArgs)
  执行一个不是SELECT / INSERT / UPDATE / DELETE

的SQL语句

改为使用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();
}

答案 1 :(得分:0)

SQL字符串用引号分隔;如果你的字符串包含引号,你必须正确地转义它们。

为避免格式化此类问题,请改用参数:

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