Android/SQLite deleteDatabase not working as expected - android

I am working on an Android app, and from time-to-time I need to wipe the SQLite database to start from a clean slate. After some googleing, I found:
context.deleteDatabase(DATABASE_NAME);
However, whenever I execute this statement, then [attempt] re-create my database, my app always starts up empty, with no data. The rest of the UI is there, it's just there is no data. To create the databse, I am using the standard
DatabaseHelper myDBHelper = new DatabaseHelper(this);
which extends SQLiteOpenHelper, that I have seen so many times here on StackOverflow.
As an FYI, when I do not execute the deleteDatabase() statement, all my data loads perfectly, so I know my DatabaseHelper class is working properly.
Also, the reason I am trying to clear the DB is because whenever I build my application in Android Studio, it always adds to the database instead of deleting it, then inserting fresh.

Related

Android Create Database instance for the entire application or class

My app has several AsyncTaskRunners which are constantly access the database. I keep getting ThreadPool errors because of a closed database instance. Originally I had a new instance opened and then closed again, each with a different name every time. So every method that accessed the database would open its very open instance do whatever and then close that instance.
I was hoping that it would have been more efficient to open a single instance and then not close it so that every method within the AsyncTask would just do its thin with the database.
Is there a way to open a database instance when the app starts and not close it, and then get & put throughout the entire application just be referencing that instance?
Or is there a way to open an instance for my AsyncTaskRunner class so that all methods in that class can talk to the database?
I have tried initializing a database instance at the beginning of the class with
SQLDatabase db;
and then later in the doInBackground() I would open an instance of db and not close it. Then the various methods would try to talk to the db but with no success.

Android SQLiteDatabase working with Emulator but not on Real Device

Firstly, what works is: A simple application that contains a sqliteDatabase. I populate this in the main activity and when the other activity is called, it queries the database and returns a string array. I put this result into a spinner using 'new ArrayAdapter'.
I don't think the code matters in this case as it works on the emulator fine. The spinner is populated ok on the emulator but won't populate on the phone?
Maybe the database doesn't even get created on the phone?
Anyone know what could be the cause of the problem? Thanks!
Your suspicion that the database isn't created might be a hint (is it likely you have been developing solely using the emulator and the database has been built in previous builds, whereas the current build doesn't create a new one when it doesn't yet exist ?).
Try logging whether the "onCreate"-method of the SQLiteDataBase class is fired when the class is instantiated. Try logging the SQL results for queries as they might indicate what is wrong (i.e. non-existing tables or column names, etc).

What would happen if I don't close the database in Android?

This title may sound a little bit crazy, but this is what is making me confused. My app heavily uses local database operations. As suggested in the Android docs and some blogs, I extended the SQLiteOpenHelper class and defined all my DB operations there. As some of my DB operations execute in threads, opening and closing of the db causes some IllegalStateExceptions. So, I made my DB helper as Singleton and it resolved those issues, also getting rid of the open and close operations for every DB action. Now everything seems to be working fine even though I never close the DB.
My confusion is that is it necessary to close DB?
If so, what is the right place to do so, is it in onDestroy of the main activity or somewhere else?
If I don't close DB, what are the side effects?
You can catch IllegalStateException if you'll try to open again the same database.
If you create instance of DBHelper in onCreate method of main activity - it would be write to close db in onDestroy. So, you can be sure, that next time in onCreate your database is not opened already.
If you have reference to DBHelper in service, than it should be opened and closed in service, and not in activity.
You can also use Application class for opening db, but than it will opened every time when you app starts (for example when you receive BroadcastReceiver)
The reason why you get exceptions is that you are trying to write/read from the same database via different threads.
I believe the best place to close your database would be inside the onDestroy() of your mainActivity.
I faced same problem.I opened database ,perform some operation and i forget to close.Actually i saw some exceptions i logcat that is "Leak found" db is opened and never closed.

Android SQLite connection management

I'm working with SQLite in my android project. There is a feature that is clear to me -- if there are multiple threads working with DB then they should use only ONE instance of DBHelper and SQLite guarantee a safe access (even if it is concurrent) to the DB.
But I still have one thing needed to be clarified. How should I manage with DB connection (SQLiteDatabase object)? How often should I call geWritableDatabase() and close()? Is it ok if I call these methods once? Or it's better to obtain SQLDatabase object and close one every time I perform read/write operation on DB?
It is handled by default... I mean if a writtable database open, whenever you try to access read-only database it closes writtable one and create new read-only database instance. As far as i know :)

android sqlite application is being forcefully closed on device

I am using SQLiteOpenHelper class to create, open database. for my application i am creating writable object SQLiteDatabase which i am using to read and write data to database. This object is static for main class and used in all application to read write in to database. my application is working properly on emulator. but on device after some read write query fires. why it is happening, please if any one has solution help me.
It is not recommended to have the helper as a static instance. Rather you should instanciate it each time you need to acces the DB. What might be happening is that your DB connection is getting closed by Android (because it needs memory, because there are too many open connections, ...) and when you do a query, you do not check that the connection is still open.
You can read this tutorial to get the idea of how to do it properly.
Basically inside an activity, the idea:
private void reloadData() {
MyDBHelper db = new MyDBHelper(this.getApplicationContext());
db.open();
Cursor c = db.query(...);
db.close();
// Update your data using the cursor
}
I had the same problem a few weeks ago, and I found this page, it's exactly what's wrong. The SQLite is buggy in Android and is implemented the wrong way, so you can't have threads reading and writing to the database/table at the same time.
setLockingEnabled() just dont work the way it's supposed.
What I did was running all methods that read and write to the db "Synchronized" which means you'll never have a problem with reading or writing data at the same time no matter how many threads you have.
Regards
Tobias

Categories

Resources