-1

I have got a database table with a column "id INTEGER PRIMARY KEY AUTOINCREMENT". When I try to update an existing row of this table with the primary key, the app exits saying "Unfortunately is stopped."

public int updateUser(User user) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_FIRSTNAME, user.getUser_firstName()); // user first name
    values.put(KEY_LASTNAME, user.getUser_lastName()); // user first name
    values.put(KEY_USERNAME, user.getUsername());
    values.put(KEY_PASSWORD, user.getPassword());
    values.put(KEY_USERLEVEL, user.getUser_level());
    values.put(KEY_DESIGNATION, user.getDesignation());
    values.put(KEY_MOBILENO, user.getMobile_no());
    values.put(KEY_EMAIL, user.getEmail());
    values.put(KEY_NIC, user.getNIC());
    values.put(KEY_GENDER, user.getGender());
    values.put(KEY_DOB, user.getDOB());
    values.put(KEY_AVAILABILITY, user.getAvailability());

    db.close();

    // updating row
    return db.update(TABLE_USERS, values, KEY_ID + " = ?",
            new String[] { String.valueOf(user.getUser_id()) });

Logcat :

E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.projectmanagementsys.com.sasith.project_management/com.projectmanagementsys.com.sasith.project_management.AddUser}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.projectmanagementsys.com.sasith.project_management/databases/ProjectManagement

There is no error in the whole thing and the code works well when I comment the update query. Can someone give a solution please ? Thank you.

  • 1
    paste the logcat of the error – Farhad Feb 16 '16 at 18:10
  • 1
    "When I try to update an existing row of this table with the primary key the app exits saying "Unfortunately is stopped."" -- use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this. Also, AFAIK, `values` cannot have `id`, because of the `AUTOINCREMENT`. Either *you* set the IDs, or SQLite does, not both. – CommonsWare Feb 16 '16 at 18:12
  • 1
    Please show how you are building the complete query. Also, the stack trace would be nice. – Droid Chris Feb 16 '16 at 18:39
  • @FarhadFaghihi I added the Logcat so can you have a look ? Thank you. – Sasith Priyankara Feb 16 '16 at 18:40
  • Where are you opening the database? – Droid Chris Feb 16 '16 at 18:42
  • `attempt to re-open an already-closed object` read your error – njzk2 Feb 16 '16 at 18:52
  • I saw the error but don't know how to solve it. I have open the database and close it once where I added the data. Can you show me how to correnct ? – Sasith Priyankara Feb 16 '16 at 19:34

1 Answers1

2

You have the line db.close() followed by db.update() and your error states that you are attempting to open an already-closed object. You haven't closed the db after you've added the data, you've closed it before. You'll need to update the row, close the database, then return your value.

// updating row
int result = db.update(TABLE_USERS, values, KEY_ID + " = ?",
        new String[] { String.valueOf(user.getUser_id()) });

db.close();
return result;
Joseph Roque
  • 5,066
  • 3
  • 16
  • 22