Search code examples
javaandroidsqliteandroid-sqlite

Android Studio sqlite update


I have made a sqlite table where I can insert data for my Trip app. I'm trying to update the rows, but I can't figure it out. I have searched alot but I'm doing something wrong on the update part and I don't know what.

public static final String DATABASE_NAME = "triptracker.db";
public static final String TABLE_NAME = "trip_table";
public static final String COL_1 = "trip_id";
public static final String COL_2 = "trip_title";
public static final String COL_3 = "trip_description";
public static final String COL_4 = "trip_image";
public static final String COL_5 = "trip_location";

When I am inserting data:

public boolean insertData(String title, String description, String location) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2, title);
        contentValues.put(COL_3, description);
        // contentValues.put(COL_4, image);
        contentValues.put(COL_5, location);
        long result = db.insert(TABLE_NAME, null, contentValues);
        if (result == -1)
            return false;
        else
            return true;

    }


//When I'm updating the database

public boolean update(String title, String description, String location){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2, title);
        contentValues.put(COL_3, description);
        contentValues.put(COL_5, location);
        db.update(TABLE_NAME,contentValues, "location=?", new String[]{location} );
        return true;

    }

// I need to press a button in order to save the data
        saveItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

              if(!editLocation.getText().toString().equals("") && !editTitle.getText().toString().equals("") && !editDescription.getText().toString().equals("")){
                    boolean insert = myDb.update(editLocation.getText().toString(),
                            editDescription.getText().toString(),
                            editTitle.getText().toString());
                }
                else {
                    Toast.makeText(singlemomentactivity.this, "Your moment is not added ", Toast.LENGTH_LONG);
                }


Solution

  • Pass the trip_id column's value as a parameter:

    public boolean update(String tripid, String title, String description, String location){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2, title);
        contentValues.put(COL_3, description);
        contentValues.put(COL_5, location);
        return db.update(TABLE_NAME,contentValues, COL_1 + " = ?", new String[]{tripid}) > 0;
    }
    

    and call the method:

    boolean insert = myDb.update(
      <pass the id's value here>,
      editTitle.getText().toString(),
      editDescription.getText().toString(),
      editLocation.getText().toString()
    );
    

    I changed the order of the parameters because it was wrong in your code.
    Also your update() method will return true if the row is updated with the condition > 0