4

In my application I need to add and edit data to the SQLite database. 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 !!

3
  • 2
    just do long i=myDB.update(DATABASE_TABLE, cv, KEY_ROWID + "=" + rowid, null); and print the i value in console. check and let us know what i print actually? Commented Nov 14, 2011 at 9:39
  • It gives 0; Is there any error in my code. I can't find it. Commented Nov 14, 2011 at 9:59
  • Maybe your transaction is causing this, try without them! Commented Nov 14, 2011 at 10:12

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Your function returns 1; that's not ok... it should return ContentValues cv

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.