Reputation: 11
I have a project with SQLite database in Android. I wish to execute few operations on it, for example save a row, read all rows from table, delete row etc. I want to execute that operations in another thread. My question is: which multitasking method should I use? I was thinking about one class, something like database manager, which has some methods that represents operations I wrote above. I don't have an idea how to make it easy and simple. Maybe Service will help me with this task or maybe should I use another class for one operation, for example one AsyncTask class for one operation? I am waiting for all of Your answers and advices. Thanks!
Upvotes: 0
Views: 834
Reputation: 396
You can use AsyncTask for this.In its doInBackground() method you can do all the operations required like adding to the contents to the table etc.
Add this in your Activity.
private class myTask extends AsyncTask<Void, Void, Bitmap> {
DataBaseHelper db=new DataBaseHelper(context);
protected void doInBackground(Void... params) {
db.addData(.....);
}
}
And the below is the DataBaseHelper class.
public class DataBaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
public static final String DATABASE_NAME = "YourName.db";
// table name
public static final String TABLE_DATA = "your Table name";
// Table Columns names
public static final String KEY_ID = "id";
public static final String KEY_NAME = "DataName";
public static final String KEY_CONTENT = "Content";
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE_DATA = "CREATE TABLE " + TABLE_DATA + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT,"
+ KEY_CONTENT + " TEXT" + ")";
db.execSQL(CREATE_TABLE_DATA);
public void addData(DataBaseModel dataBaseModel) {
SQLiteDatabase db1 = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, dataBaseModel.getDataName()); // Contact Name
values.put(KEY_CONTENT, dataBaseModel.getContent()); // Contact Phone
// Inserting Row
db1.insert(TABLE_DATA, null, values);
//Toast.makeText(context, "New row added, row id: " , Toast.LENGTH_SHORT).show();
db1.close(); // Closing database connection
}
// Getting single data
public String getData(String dataName) {
String data=null;
SQLiteDatabase db = this.getReadableDatabase();
//SQLiteDatabase db2=SQLiteDatabase.openDatabase(DB_PATH + DATABASE_NAME, null, SQLiteDatabase.CREATE_IF_NECESSARY);
Cursor cursor = db.query(TABLE_DATA, new String[]{KEY_ID,
KEY_NAME, KEY_CONTENT}, KEY_NAME + "=?",
new String[]{dataName}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
try {
data = cursor.getString(cursor.getColumnIndex(KEY_CONTENT));
}catch (Exception e){
e.printStackTrace();
data=null;
}
cursor.close();
return data;
}
Upvotes: 0
Reputation: 4798
I would think a background service is a bit overkill for simple database operations.
For the most part, I'd use a ListView with a Cursor Adapter for list type data that was required to scroll. For smaller data sets, maybe< 10 rows, I'd just fetch the data in the UI thread.
For more complex operations, say checking the state of the DB on startup, or removing a set of rows, I would write an AsyncTask for each purpose.
I have been using ORM Lite for just about all my Android SQL lite projects for a while now and I just follow the convention of a singleton DatabaseManager and create methods within the manager class that require custom queries.
Upvotes: 1