【问题标题】:Update Function in android SQLite is not workingandroid SQLite 中的更新功能不起作用
【发布时间】:2011-11-14 09:35:33
【问题描述】:

在我的应用程序中,我需要向 SQLite 数据库添加和编辑数据。当我更新数据功能应用程序时不会给出任何错误,但我的数据库不会更新。这是我的更新功能。我在网上搜索了两天,但无法做到这一点。请帮帮我。

public long updateEvent(String id, String title, String description, String reminder, String alarm, Date date) {

    try {
        int rowid = Integer.parseInt(id);
        Log.i("com.eventmanager", "insert Event");
         formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         String datestring = formatter.format(date);

        ContentValues cv = new ContentValues();
        cv.put(KEY_TITLE, title);
        cv.put(KEY_DESCRIPTION, description);
        cv.put(KEY_REMINDER, reminder);
        cv.put(KEY_ALARM, alarm);
        cv.put(KEY_DATE, datestring);
        myDB.beginTransaction();

        Log.i("com.eventmanager","SQL UPDATE ");

        myDB.update(DATABASE_TABLE, cv, KEY_ROWID + "=" + rowid, null);
        myDB.setTransactionSuccessful();

        myDB.endTransaction();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return 1;
}

提前致谢!!

【问题讨论】:

  • 只需执行long i=myDB.update(DATABASE_TABLE, cv, KEY_ROWID + "=" + rowid, null); 并在控制台中打印i 值。检查并告诉我们i 实际打印的内容?
  • 它给出 0;我的代码中是否有任何错误。我找不到它。
  • 可能是您的交易造成的,请尝试不使用它们!

标签: android sqlite sql-update


【解决方案1】:

可能是更新语句有问题,试试:

 long i = myDB.update(DATABASE_TABLE, cv, KEY_ROWID + "=?", new String[]{rowid});

 if(i>0)
     return 1;  // 1 for successful
 else
     return 0;  // 0 for unsuccessful

是的,通过public int update (String table, ContentValues values, String whereClause, String[] whereArgs) 函数。

【讨论】:

    【解决方案2】:

    你的函数返回 1;不行……它应该返回 ContentValues cv

    return db.update(DATABASE_TABLE, cv, KEY_ROWID+"="+rowId, null)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多