Android – SQLite insert, delete and tip

You can read about SQLiteOpenHelper here.

insert

To insert a record in a table you need:

  • a set of values using ContentValues class
  • the table name
SQLite insert delete, <span>Android – SQLite insert, delete and tip</span>, KandZ

delete

To delete a record from a table you need:

  • the table name
  • selection string
  • selection arguments array
//selection String
String selection = _ID + " = ?";
//Selection Arguments String array
String[] selectionArgs = {Integer.toString(Id)};

SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.delete(TABLE_NAME,selection,selectionArgs);

Tip

It is not recommended to execute any database operation on the Main/UI thread. So use AsyncTask, override the doInBackground method and execute the database operations.

final String selection = _ID + " = ?";
final String[] selectionArgs = {Integer.toString(Id)};

        AsyncTask task = new AsyncTask() {
            @Override
            protected Object doInBackground(Object[] objects) {
                SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
                db.delete(TABLE_NAME,selection,selectionArgs);
                return null;
            }
        };

        task.execute();

THE END!!!

99120cookie-checkAndroid – SQLite insert, delete and tipAndroid – SQLite insert, delete and tip

Related posts