【问题标题】:Updating table not working properly更新表无法正常工作
【发布时间】:2011-08-02 07:56:51
【问题描述】:

我正在尝试使用以下代码,但无法更新我的表格。

db = dbHelper.getWritableDatabase();
int i = 1;
String updateStatement = "update "+MESSAGE_TABLE+" SET status ="+i+" where message_id = "+message_id;
SQLiteStatement update;
update = db.compileStatement(updateStatement);
update.executeInsert();

我也尝试了update.executeI(),而不是update.executeInsert(),但它也不起作用。

【问题讨论】:

  • 如果您附上错误的 logcat 输出,它将帮助其他人诊断您的问题
  • 在 log cat 中显示数据已更新。但是当我从表中检索数据时......数据没有更新
  • 你确定executeUpdateDelete()的返回值是1?
  • 我没用过executeUpdateDelete()。但是,我得到了我的解决方案……感谢您的支持。如果可能,请给我链接,其中包含此命令的示例executeUpdateDelete()

标签: android sqlite


【解决方案1】:

希望对你有帮助

db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);    

ContentView update = new ContentView();
update.put("fieldNameToUpdate","Value Here as a String " or integer);
db.update("table", update,/*where clause*/ "id=2",null);

db.close();

这对我有用。 您可以根据需要更改值。

谢谢 嘘..

【讨论】:

    【解决方案2】:

    SQLiteStatement 中使用executeUpdateDelete() 方法并好好阅读文档。 http://developer.android.com/reference/android/database/sqlite/SQLiteStatement.html

    【讨论】:

      【解决方案3】:

      如果您想将SQLiteStatementexecuteUpdateDelete() 方法一起使用,则可以:

      String sql = "UPDATE table_name SET column_2=? WHERE column_1=?";
      SQLiteStatement statement = db.compileStatement(sql);
      
      int id = 7;
      String stringValue = "hi there";
      
      statement.bindString(1, stringValue);
      statement.bindLong(2, id); // These match to the two question marks in the sql string
      
      int numberOfRowsAffected = statement.executeUpdateDelete();
      

      注意:executeUpdateDelete() 是在 API 11 中引入的。See this Q&A

      Fuller explanation on how to use prepared statements:

      【讨论】:

      • 这个答案救了我的命!
      【解决方案4】:
      【解决方案5】:

      我认为如果其他一切都正确,问题就在您的查询中

      问题可能出在字段类型上。假设message_id不是数字。那么使用like

       String updateStatement = "update "+MESSAGE_TABLE+" SET status ="+i+" where message_id = '"+message_id+"'";
      

      字段状态相同,如果不是数字类型必须使用''

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-07
        • 2017-08-14
        • 1970-01-01
        • 2016-03-03
        • 2022-08-02
        • 2019-08-10
        • 2018-12-09
        • 2016-11-25
        相关资源
        最近更新 更多