Solution 1 :
However, I wish to ask if the delete can be used only in Main Activity or only in the DBHelper?
Neither, it could be undertaken in either and even both.
The DBHelper’s (DBAdapter or whatever you wish to call it) job is primarily to provide a means of controlled opening of the database, creating it if it doesn’t exist (in which case the overidden onCreate
method is called) and also checking the user_version (stored in the file’s header) against a coded version (calling the overidden onUpgrade
method, or in more obscure circumsyances the onDowngrade
method).
From the DBHelper you can get an SQLiteDatabase instance via the getWritableDatabase
or the getReadableDatabase
methods. It is this SQliteDatabase object that you use to insert/delete/update/query.
The SQLitedatabase provides various methods of undertaking the access actions. Such as execSQL, rawQuery and many convenience methods such as insert, update, delete and query.
The convenience methods can simplify matters, often returning suitable usable values (e.g. the convenience insert method returns a long which is the value of the rowid of the inserted row, query returns a Cursor object which is the output values).
So all you require is an SQLiteDatabase object.
Example
Perhaps consider the following example, that DELETE’s rows (albeit that there are none to delete) via both the DBHelper and via the MainActivty.
The Database Helper AHelperForTheDatabase
- named different to what is typically seen, just to show that the name is irrelevant.
- utilises a singleton approach
- has an method deleteById that can be used from an instance the the class.
:-
class AHelperForTheDatabase extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "the_database.db";
public static final int DATABASE_VERSION = 1;
public static final String MYTABLE_TABLENAME = "mytable";
public static final String MYTABLE_ID_COLUMN = BaseColumns._ID;
public static final String MYTABLE_NAME_COLUMN = "_name";
private static volatile AHelperForTheDatabase INSTANCE_OF_AHELPERFIRTHEDATABASE=null;
private AHelperForTheDatabase(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
public static AHelperForTheDatabase getInstanceOfAHelperForTheDatabase(Context context) {
if (INSTANCE_OF_AHELPERFIRTHEDATABASE == null) {
INSTANCE_OF_AHELPERFIRTHEDATABASE = new AHelperForTheDatabase(context);
}
return INSTANCE_OF_AHELPERFIRTHEDATABASE;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + MYTABLE_TABLENAME +
"(" +
MYTABLE_ID_COLUMN + " INTEGER PRIMARY KEY" +
"," + MYTABLE_NAME_COLUMN + " TEXT " +
")" +
"");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {}
public long deleteById(long id) {
// Convenience DELETE //
return this.getWritableDatabase().delete(MYTABLE_TABLENAME,MYTABLE_ID_COLUMN + "=?",new String[]{String.valueOf(id)});
}
}
And MainActivity which includes 4 different ways (see comments) to delete rows (again now rows are actually delete as none exist) :-
public class MainActivity extends AppCompatActivity {
AHelperForTheDatabase TheDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TheDatabaseHelper = AHelperForTheDatabase.getInstanceOfAHelperForTheDatabase(this);
TheDatabaseHelper.deleteById(919191); /* DELETE via method defined as part of the Database Helper */
/* DELETE via execSQL method nothing returned to indicate success or not */
TheDatabaseHelper
.getWritableDatabase()
.execSQL(
"DELETE FROM "
+ AHelperForTheDatabase.MYTABLE_TABLENAME
+ " WHERE " + AHelperForTheDatabase.MYTABLE_ID_COLUMN + "=?",
new String[]{String.valueOf(919191)}
);
/* DELETE via delete convenience method - (can return number of affected rows) */
TheDatabaseHelper
.getWritableDatabase()
.delete(
AHelperForTheDatabase.MYTABLE_TABLENAME,
AHelperForTheDatabase.MYTABLE_ID_COLUMN+"=?",
new String[]{String.valueOf(919191)}
);
/* DELETE via the rawQuery method (shouldn't really be used as does the additional of handling a Cursor for returning output data) */
TheDatabaseHelper
.getWritableDatabase()
.rawQuery(
"DELETE FROM "
+ AHelperForTheDatabase.MYTABLE_TABLENAME
+ " WHERE " + AHelperForTheDatabase.MYTABLE_ID_COLUMN + "=?",
new String[]{String.valueOf(919191)}
);
}
}
- With such flexibility, you will obviously come across, the flexibility used in different ways.
- Note that in ALL cases (albeit it hidden away in the delete convenience method) the actual value is bound that is the actual SQL has a
?
, which is replaced by the value, by SQLite parameter binding. This is considered the safe why as it protects against SQL Injection.
Problem :
I managed to find good examples where the android development team failed to provide. However, I wish to ask if the delete can be used only in Main Activity or only in the DBHelper? I’m unsure if DBHelper.java might be called DBAdapter as some examples seem to show. The specific syntax I’m referring to is this which you can certainly find using an internet search. However, not many recent examples are available and many I found in the past months / years have too many files and I’m not an expert:
DB.rawQuery(“Select * from Userdetails where name = ?”, new String[]
Comments
Comment posted by android-examples.com/…
I tried to add the delete syntax to this example but it won’t work: