SQLite usage from activity and service - android

I have created a databaseprovider class which uses single instance of db object. Object is created in main activity and closed onDestroy method. This seems ok (but get some errors such as: db already closed or db is not open on some users devices that I cannot simulate).
I want to add a service to the application for the content download and this service can run with scheduler which make me think about single instance of db object will not work. Should I use another object for the service, will it result consistency problems? Can you kindly advice what would be the best way?
Databaseprovider class exm:
public class DatabaseProvider {
private static DatabaseHelper helperWriter;
public static SQLiteDatabase db_global;
public DatabaseProvider(Context c) {
helperWriter = DatabaseHelper.getHelper(c, true);
}
private static SQLiteDatabase getDB() {
if(db_global == null)
db_global = helperWriter.getWritableDatabase();
else if(!db_global.isOpen()) {
try {
db_global.close();
}
catch(Exception ex) {
ex.printStackTrace();
}
db_global = helperWriter.getWritableDatabase();
}
return db_global;
}
public String GetVersion() {
SQLiteDatabase db = getDB();
Cursor c = db.query(DatabaseHelper.PARAMETER_TABLE_NAME, new String[] {"VALUE"}, "KEY='Version'", null, null,null,null);
String version = "";
if(c.moveToNext())
{
version = c.getString(0);
}
else
version = "0";
c.close();
return version;
}
public long UpdateVersion(String value) {
ContentValues initialValues = new ContentValues();
initialValues.put(DatabaseHelper.PARAMETER_COLUMN_VALUE, value);
SQLiteDatabase db = getDB();
long r = db.update(DatabaseHelper.PARAMETER_TABLE_NAME, initialValues, "KEY='Version'", null);
if(r <= 0)
r = helperWriter.AddParameter(db, "Version", value);
//db.close();
return r;
}
public void CloseDB() {
if (db_global != null)
db_global.close();
db_global = null;
helperWriter.close();
}
}

Not sure if this will help, but...
you can't rely on onDestroy() in case the app crashes. Android may also keep your app in RAM, even if you exit it. Also, your main activity may get destroyed while the app is getting used if you are on a subactivity. It can also get recreated.
Sometimes it's better to have calls that open the DB, does stuff to it, and then closes it within the same function. If you are using a service, it may actually help things. I also am not sure if you should have a situation where a DB can be opened and/or accessed from a variety to different places at once without some management code

I see a couple questions:
A)
(but get some errors such as: db already closed or db is not open on some users devices that I cannot simulate).
...
Start an activity, then update content and some db operations in AsyncTask. While update is in progress go back and start the same activity again.
To work around these errors have you considered using a [Loader][1]? It's a callback based framework around ContentProviders.
B)
add a service to the application for the content download and this service can run with scheduler which make me think about single instance of db object will not work. Should I use another object for the service, will it result consistency problems?
This post by #commonsware from this website, suggests not to use Service for long running tasks. Instead the AlarmManager is suggested. I've only worked with short running services (for audio IO) myself.

Related

Closing Android databases across Parallel Threads

I am asking this question in context of a problem in my app, about which I find it difficult to create an exact question. But I do have a lead.
I do have parallel threads running and my problem revolves around the case where running queries on database returns NullpointerException on the initialized database instance.
So what I want to know is that if you initialize an instance of a database by db.getWritableDatabase() in 2 parallel threads, does closing the database in one thread by db.close(), closes it in the other thread ? infact across the application level ?
You should create singleton of SQLiteOpenHelper/db (you did not specify what class db is) which would return you only one instance and then you could check if db is closed or not.
I had similar problem and in the end 2 parallel threads and 1 database ? You are asking for problems.
You cannot safely have 2 actions operating with database at the same time.
/**
* Returns a writable database instance in order not to open and close many
* SQLiteDatabase objects simultaneously
*
* #return a writable instance to SQLiteDatabase
*/
public SQLiteDatabase getMyWritableDatabase() {
if ((db == null) || (!db.isOpen())) {
db = this.getWritableDatabase();
}
return db;
}
#Override
public void close() {
super.close();
if (db != null) {
db.close();
db = null;
}
}

How do I access the same database from different activities?

I am writing an app and I want to store the high scores. I have it working to show the high score on the end activity. However, I want to have a highscores activity to show all the highscores. I am doing this by, in the highscores activity, calling the end activity to return the high score, so the database doesn't change. After running a debug, I saw that it got to the databasehandler but got caught on getReadableDatabase(), saying that it was unable to invoke the method on a null object reference.
This is my highscores method(I didn't include the whole thing, and the ifs are because there are different difficulties of the game)
public class highscores extends Activity {
TextView mode, score;
Button right, back;
int modenum;
end get=new end();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_highscores);
mode =(TextView)findViewById(R.id.scorebar);
score =(TextView)findViewById(R.id.score);
right =(Button)findViewById(R.id.button14);
back =(Button)findViewById(R.id.highscores);
mode.setText("Easy");
score.setText(get.datatostring(1));
modenum =1;
}
public void right(View view){
if(modenum==1) {
modenum = 2;
mode.setText("Hard");
score.setText(get.datatostring(2));
}else if(modenum==2) {
modenum = 3;
mode.setText("X-Mode");
score.setText(get.datatostring(3));
}else {
modenum = 1;
mode.setText("Easy");
score.setText(get.datatostring(1));
}
}
This is in the end method
public String datatostring(int difficulty){
MyDBHandler db = new MyDBHandler(this, null,null,1);
return db.datatostring(difficulty);
}
And this is in the databasehandler
public String datatostring(int difficulty){
SQLiteDatabase db = getReadableDatabase();
String dbString = "";
String c;
if(difficulty==1)
c = COLUMN_SCORE;
else if(difficulty==2)
c = COLUMN_HARD;
else
c = COLUMN_X;
String query = "SELECT "+c+" FROM "+TABLE_SCORES;
Cursor cursor = db.rawQuery(query,null);
cursor.moveToFirst();
if(!cursor.isAfterLast()){
int index = cursor.getColumnIndex(c);
String value = cursor.getString(index);
if(value!=null){
dbString += cursor.getString(cursor.getColumnIndex(c));
}
}
db.close();
cursor.close();
return dbString;
}
I suggest to use a SQLiteOpenHelper to handle this.
You should see this example:
Android Sqlite DB
Hope that helps, i personally works in this way.
The other option is to use som ORM, i recommend to use GreenDao, its good, and lets handle easy way this kind of actions.
Regards.
Follow this tutorial to create a concurrent and scalable database
Remember :
You should always make database queries through one instance (Singleton) of your database adapter else you will face a lot of issues when accessing your database concurrently from different classes.
Use SQLiteOpenHelper class for accessing your database. As it gives you many useful functions eg. upgrading user's database when you publish app updates with schema changes.
The short answer is yes.
But as you see you have no clear concept of the connection.
Bd in the android is SQLite (recommendation) Always be on the same route, you need to create a class that allows you to manage and connect to the database. The class will be SQLiteOpenHelper
Check THIS
You can connect one or more times to the database, since activity A, B or activity you want.
The important thing is to define the handler to connect, close, make request to the database.

Android - Running multiple Database Instances

I am developing an android app. In that app, I have to add and get the data from my database constantly.
I using three separate threads to do so. I do not think I have a thread problem as in my DDMS I can monitor the threads opening But what I think, I am failing to grasp is the Database instances.
For example,
I have a method which shall construct a filename which is made up of the value in a cell of one table + "_" + value of a cell in another table. So, I have this method which calling two other methods each with the task to go to the database and get that value.
Problem is that I am not sure how I should create an instance.Below you can see that for each method I have created a separate instance of the same database and then closed that.
In this AsyncTaskRunner class I have many methods of the same as below which are doing their own task, but open a different instance name for the same database each time.
This seems very wrong.I would imagine that as soon as the class opens I could open ONE single instance of the database and then not close it until Destroy() so that all methods can do their thing.
What can I do better?
Here is my code :
public String evaluateATable(String filenamePrefix){
SQLDatabase getATableData = new SQLDatabase(mContext);
getATableData.open();
String aRowId = SQLDatabase.evalATable(filenamePrefix);
getATableData.close();
if(aRowId != null){
return aRowId;
}
return null;
}
public String evaluateLTable(String filenamePrefix){
SQLDatabase getLtabledata = new SQLDatabase(mContext);
getLtabledata.open();
String lRowId = SQLDatabase.evalLTable(filenamePrefix);
getLtabledata.close();
if(lRowId != null){
return lRowId;
}
return null;
}
you can do one thing ,you have to declare SQLDatabase and create instance of it once, and then use that in all method
just like below
Public class test extends Activity {
SQLDatabase getATableData;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLDatabase getATableData = new SQLDatabase(this);
}
public String evaluateATable(String filenamePrefix){
getATableData.open();
String aRowId = SQLDatabase.evalATable(filenamePrefix);
getATableData.close();
if(aRowId != null){
return aRowId;
}
return null;
}
public String evaluateLTable(String filenamePrefix){
getLtabledata.open();
String lRowId = SQLDatabase.evalLTable(filenamePrefix);
getLtabledata.close();
if(lRowId != null){
return lRowId;
}
return null;
}

How to force serial/sequential execution of AsyncTasks in Android 2.x

There are a few Android APIs (after donut and before honeycomb) if Im not mistaken, where Google have enabled the AsyncTasks to run paralelly aiming for faster execution. Then lots of devs made mistakes when reaching out to the same database using multiple AsyncTasks, and since Android 3.0 AsyncTasks are running serially by default.
I am suffering this problem now when testing my app on an Android 2.3.4 device with my SQLite
First, Im getting categories from the server, I open DB, insert them close DB.
Second I get the subcategories from the server, open DB, insert them into DB, close DB
Third I get user items from the server, open DB, insert items, then close DB
Im taking good care to ensure that one starts after another, but in every 8-10 iterations something somewhere slows down and overlaps with another procedure right in the moment where a task is opening the db, another task closes it right after, and the first task starts trying to write to a closed db....
What do I do? I want clean, reliable separation, sequential execution and I dont want to start the asynctasks from the previous asynctask's onPostExecute, because these three will not always run in a row
I read an article yesterday that you CANT do it on android 2.x
Shall I try to open the DB and DBHelper before ALL of the operations and close the DB afterwards?
EDIT: Usually I get the error here (at Begin transaction):
(The error says that the DB is closed)
#Override
protected Void doInBackground(Void... arg0) {
// dbTools.close();
try {
if (database == null) {
database = dbTools.getWritableDatabase();
}
} catch (Exception e) {
e.printStackTrace();
}
ContentValues values = new ContentValues();
database.beginTransaction();
try {
// Iterating all UserItem objects from the LinkedHashSet and getting their info
for (UserItem userItem : userItems) {
// Inserting values for the database to insert in a new record
values.put(C.DBColumns.ITEM_ID, userItem.getItemId());
values.put(C.DBColumns.ITEM_NAME, userItem.getItemName());
// database.insertWithOnConflict(C.DBTables.ITEMS, null, values, SQLiteDatabase.CONFLICT_REPLACE);
database.insert(C.DBTables.ITEMS, null, values);
} // End of For loop
database.setTransactionSuccessful();
} finally {
database.endTransaction();
}
// Closing all cursors, databases and database helpers properly because not closing them can spring lots of trouble.
if (database != null && database.isOpen()) {
try {
database.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
} // End of doInBackground
And this is my DBTOOLS CLASS:
public class DBTools extends SQLiteOpenHelper {
// Its a good practice for DBTools to be a singleton. Do not instantiate it with "new DBTools(context)" but with
// DBTools.getInstance(context) instead
private static DBTools sInstance;
public static DBTools getInstance(Context context) {
if (sInstance == null) {
sInstance = new DBTools(context);
}
return sInstance;
}
public DBTools(Context context) {
super(context, C.Preferences.LOCAL_SQLITE_DATABASE_NAME, null, 1);
}
public void onCreate(SQLiteDatabase database) {
database.execSQL(SQLQueries.tableCategoriesCreate);
database.execSQL(SQLQueries.tableSubcategoriesCreate);
database.execSQL(SQLQueries.tableItemsCreate);
}
public void onOpen(SQLiteDatabase database) {
database.execSQL(SQLQueries.tableCategoriesCreate);
database.execSQL(SQLQueries.tableSubcategoriesCreate);
database.execSQL(SQLQueries.tableItemsCreate);
}
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
database.execSQL(SQLQueries.tableCategoriesDrop);
database.execSQL(SQLQueries.tableSubcategoriesDrop);
database.execSQL(SQLQueries.tableItemsDrop);
onCreate(database);
}
} // End of Class
Since you can't call from onPostExecute, I would say you have two options, one would be to move your open close calls to the beginning and end of your activity or service.
Option two would be to setup a reference counter in your DB and DBHelper where you track the number of times open has been called, and then decrement that count when close is called. That way you can perform close only when the count is 0. One thing to remember when taking this approach is that you should probably have a method that will force the db to close that you call when you are sure your other connections are done. This shouldn't be necessary but will be a failsafe to ensure the db gets closed if something goes wrong.
Edit: You would have to make DBTools a singleton for it to work, but it's not equivalent. Here's a quick example.
public class DBTools {
private static DBTools instance;
private static int openCount;
public DBTools getInstance() {
if (instance == null) {
instance = new DBTools();
}
return instance;
}
private DBTools() {
openCount = 0;
}
public void open() {
openCount++;
//Do open
}
public close() {
openCount--;
if (openCount == 0) {
//Do close
}
public void forceDBClose() {
//Do close
}
}
I am also a newbee in android. I was having a problem like this too.
To overcome this, i used Singleton class.
I created one instance of the DBHelper class and used it in all my asynctasks.
So, until the DB is closed, all the asynctasks access the initialised DB object.
If there is no object in the memory, the async tasks, instantiates it and use it then.

upgrading SQLite DB in Android using ContentProvider

I'm developing an app and using SQLite, contentResolver and contentProvider.
Application description
My app searches for contacts in internal SQLite DB. the data arrives from external file that is selected on first lunch or when the user press the update option in the menu.
All access to the DB are done using getContentResolver().
ContentProvider
I have a ContactsContentProvider class that extends ContentProvider and holds a reference to ContactsDBAdapter which is my database adapter that extends SQLiteOpenHelper.
(I hope you are all with me until now ).
Problem description
When a user press the update button I want the DB to drop all tables and load the new data (this is done by my file chooser and works great).
in order for the onUpgrade() in my ContactsDBAdapter to work the content provider onCreate() must be called with a higher version then what it had before
#Override
public boolean onCreate() {
context = getContext();
pref = PreferenceManager.getDefaultSharedPreferences(context);
int dbVersion = pref.getInt(Settings.DB_VERSION, 1);
mDb = new ContactsDBAdapter(context,dbVersion);
return (mDb == null)? false : true;
}
But I get the contentProvider from my contentResolver so it is not created twice.
Although there are a lot of explanation of how to use both contentProvider and contentResolver I didn't find anywhere a good upgrade progress.
I'm aware of how the onUpgrade works and that it is being checked during getReadableDatabase() and getWritableDatabase() calls but the fact is that the version will not be diferent since the ContactsDBAdapter is the same instance as it previously was.
I thought about some work arounds but didn't like them at all.
I can manually check during insert() if the version is higher (but that would be expensive since it is done by every call) and if the answer is true then manually call onUpgrade.
or to try and unregister the Provider in some way but didn't find any valid and good solution so far.
What is the best practice to upgrade your DB ?
Thanks!
I would suggest you update the shared preferences to change the db version and add it:
SharedPreferences.Editor ed = pref.edit();
ed.putInt(Settings.DB_VERSION, dbVersion + 1);
ed.commit();
Hopefully it helps you
I found a nice solution.
In my ContactsContentProvider which extends ContentProvider I added a sharedPrefencesListener
#Override
public boolean onCreate() {
context = getContext();
pref = PreferenceManager.getDefaultSharedPreferences(context);
int dbVersion = pref.getInt(Settings.DB_VERSION, 1);
mDb = new ContactsDBAdapter(context,dbVersion);
pref.registerOnSharedPreferenceChangeListener(sharedPrefListener) ;
return (mDb == null)? false : true;
}
SharedPreferences.OnSharedPreferenceChangeListener sharedPrefListener =
new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if(key.equals(Settings.DB_VERSION)){
int dbVersion = sharedPreferences.getInt(Settings.DB_VERSION, 1);
synchronized (mDb) {
mDb.close();//Not sure this is the right behavior
mDb = new ContactsDBAdapter(context,dbVersion);
}
}
}
};
Now when the user changes the version number in my main activity then I set the version number. This will call a new ContactsDBAdapter which will then invoke onUpgrade the next time someone will want getReadableDatabase() or getWriteableDatabase().

Categories

Resources