简体   繁体   English

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

[英]Update Function in android SQLite is not working

In my application I need to add and edit data to the SQLite database. 在我的应用程序中,我需要向SQLite数据库添加和编辑数据。 When I do update data function app do not give any errors but my database wont update. 当我更新数据功能应用程序不提出任何错误,但我的数据库不会更新。 Here is my Update function. 这是我的更新功能。 I search in the web for two days but couldn't make this. 我在网上搜索了两天但是无法做到这一点。 Please help me. 请帮我。

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;
}

Thanks in advance !! 提前致谢 !!

There may be a problem in the update statement, Try: 更新语句中可能存在问题,请尝试:

 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

And yes go through the public int update (String table, ContentValues values, String whereClause, String[] whereArgs) function. 并且是通过public int update(String表,ContentValues值,String whereClause,String [] whereArgs)函数。

Your function returns 1; 你的函数返回1; that's not ok... it should return ContentValues cv 那不行......它应该返回ContentValues cv

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM