android SQLite中的更新功能不起作用

时间:2011-11-14 09:35:33

标签: android sqlite sql-update

在我的应用程序中,我需要向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;
}

提前致谢!!

2 个答案:

答案 0 :(得分:7)

更新语句中可能存在问题,请尝试:

 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)函数。

答案 1 :(得分:0)

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

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