This is the fourth part of SQLite for Android series tutorials with examples. Here you can find the first part – SQLite for Android Part 3: Show Values from Database.
In this example is explained hot to update data previously inserted in to created database and table.
Layout
Add a edit text and button to the activity_main.xml layout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<TextView android:id="@+id/id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginTop="30dp" android:text="@string/id" android:textSize="14sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/gender" /> . . . <EditText android:id="@+id/editId" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_marginEnd="20dp" android:layout_marginBottom="5dp" android:hint="@string/id" android:imeOptions="actionNext" android:inputType="text" android:maxLength="30" android:maxLines="1" android:paddingStart="10dp" android:paddingEnd="10dp" android:textSize="14sp" app:layout_constraintBottom_toBottomOf="@id/id" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@id/editDateOfBirth" app:layout_constraintTop_toTopOf="@id/id" /> . . . <Button<br> android:id="@+id/update" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="@string/update" app:layout_constraintEnd_toEndOf="@id/addButton" app:layout_constraintStart_toStartOf="@id/addButton" app:layout_constraintTop_toBottomOf="@id/viewAll" /> |
Update method
Then we add updateData() method in our DataBaseHelper class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public boolean updateData(String id, String name, String lastName, String dateOfBirth, String gender) { SQLiteDatabase sqLiteDatabase = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(COL_1_ID, id); contentValues.put(COL_2_NAME, name); contentValues.put(COL_3_LAST_NAME, lastName); contentValues.put(COL_4_DATE_OF_BIRTH, dateOfBirth); contentValues.put(COL_5_GENDER, gender); try { sqLiteDatabase.update(STUDENT_TABLE, contentValues, "ID = ?", new String[]{id}); return true; } catch (SQLiteException e) { Log.d("SQLite", e.getMessage()); return false; } } |
Here we initiate local db object with our created database and update it with new values for designated row. We know what row to be updated by the id value that we receive as parameter in this method. ContentValues object is explained in the previous post. With .update method we are telling the database (STUDENT_TABLE), to add content value wher ID is equal to id that we receive in this method as a parameter. Every record (row) in the table have uniqe id value. ID = ? is weare clause which literally meas if ID = new String[](id) update that record. We surround this with try/catch because the update might be unsuccessful. and In that case we return false and tell the user that the record is NOT updated.
Update data implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@OnClick(R.id.update) public void onUpdateClick() { if (!TextUtils.isEmpty(editName.getText().toString()) && !TextUtils.isEmpty(editLastName.getText().toString()) && !TextUtils.isEmpty(editDateOfBirth.getText().toString()) && !TextUtils.isEmpty(editId.getText().toString()) && !TextUtils.isEmpty(editGender.getText().toString())) { boolean isUpdated = dataBase.updateData(editId.getText().toString(), editName.getText().toString(), editLastName.getText().toString(), editDateOfBirth.getText().toString(), editGender.getText().toString()); if (isUpdated) { Toast.makeText(this, "Record with id " + editId.getText().toString() + " updated", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Record with id " + editId.getText().toString() + " NOT updated!", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "Please fill all fields", Toast.LENGTH_SHORT).show(); } } |
ID field is added that we know exactly which row to be updated. If the update is successful we receive confirmation message.