I have an app that functions properly and does not force close or crash. But when I look at LogCat, it occasionally gives me this:
05-20 15:24:55.338: E/SQLiteDatabase(12707): close() was never explicitly called on database '/data/data/com.---.--/databases/debt.db'
05-20 15:24:55.338: E/SQLiteDatabase(12707): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
a little ways down...
05-20 15:24:55.338: E/System(12707): Uncaught exception thrown by finalizer
05-20 15:24:55.338: E/System(12707): java.lang.IllegalStateException: Don't have database lock!
I am not sure when I should be opening and closing my Database?
I have a Main activity that is simply a splash screen. It then goes into an activity that calls a ListView using info from the DB; so it is at this activity where the DB is first opened.
There is also one other Activity where the DB is required that branches off the one with the ListVeew. When am I supposed to be opening and closing this? Word seems to be that I simply need to open once, and then close when the app is "paused", "stopped" or "destroyed".
If this is the case, where do I put the db.close() method... in the Splash Screen Main Activity where onStop, etc is located? or the same Activity as the one that opens the DB? or.. is there another place?
UPDATE:
This is the line in code that the error keeps pointing to:
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
If you're using an instance of a DatabaseHelper class, and after you initialize the DBHelper object, every time you do work in the database you should call the open method before you do work, then create a new cursor, query the database, do work with the information you just stored in the cursor, when you're done close the cursor, then close the database. For example if you wanted to grab every item in a database you would do something like :
...
DataBaseHelper db = new DataBaseHelper(this);
...
db.open();
Cursor cursor = db.getAllItems();
maxCount = cursor.getCount();
Random gen = new Random();
row = gen.nextInt(maxCount); // Generate random between 0 and max
if (cursor.moveToPosition(row)) {
String myString = cursor.getString(1); //here I want the second column
displayString(myString); //private method
}
cursor.close();
db.close();
getAllItems is a public method in my DatabaseHelper, it looks like this in case you were wondering
public Cursor getAllItems() {
return db.query(DATABASE_TABLE,
new String[] {
KEY_ROWID,
KEY_NAME
},
null,
null,
null,
null,
null);
}
This is how I access my database and I haven't gotten any of the errors you've got, and it works perfectly.
I used to do the way #Shikima mentioned above but in complex applications which has many background services, multi-threading,etc it can get real tiresome when you have to manage many database instances and on top of that, opening and closing them.
To overcome this, I used the following method and it seems to be working fine.
1.
Declare and initialize an instance of YourDBHelperClass in your Application base class like this :
public class App extends Application {
public static YourDBHelperClass db;
#Override
public void onCreate() {
super.onCreate();
db = new YourDBHelperClass(getApplicationContext());
db.open();
}
}
2.
In you activity, or any other place you want to use the DB, initialize the YourDBHelperClass object like this :
YourDBHelperClass db = App.db;
And then you can use the database anyway you want without having to worry about opening and closing it manually each time. The SQLiteOpenHelper takes care of the closing when the Application is destroyed
You are probably not handling your database correctly; you are opening more database instances than you are closing.
There are a number of design patterns you can follow to correct this behavior. You might want to consult this answer for more information.
Related
I have an issue where my database handle is being closed during my test and I am not sure why.
The #Before method runs correctly, but the #Test method throws an exception : attempt to re-open an already-closed object. I am not closing it anywhere in my code, so I am not sure how it is getting closed.
The method marked with #Test is the only one in the test so far, so no other tests are running before it.
Can anyone help me understand what is happening here?
Test
#RunWith(AndroidJUnit4.class)
public class DatabaseTest {
private Database subject;
private SQLiteDatabase wDB;
public DatabaseTest(){
DbHelper helper = new DbHelper(InstrumentationRegistry.getTargetContext());
subject = new Database(helper);
wDB = helper.getWritableDatabase();
}
// remove any preexisting records from the database
#Before
public void cleanSlate(){
wDB.delete(Database.ITEMS_TABLE, null, null);
}
#Test
public void testInsert(){
Cursor c;
Item i = getMockItem();
subject.update(i);
// (per below) DatabaseTest.java:46:
c = wDB.query(Database.ITEM_TABLE,Database.ALL_COLUMNS,null,null,null,null,null);
//...
Error
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/com.example.app.debug/databases/storage.db
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257)
at com.example.app.data.local.DatabaseTest.testInsert(DatabaseTest.java:46)
Update Method
public void update(Item item){
SQLiteDatabase wDB = helper.getWritableDatabase();
ContentValues cv = entryToContentValues(item);
wDB.insertWithOnConflict(ITEM_TABLE, null, cv, SQLiteDatabase.CONFLICT_REPLACE );
wDB.close();
}
The issue is that you are closing the database in the update method and thus the database is closed when trying to run the query at line 46.
One solution would be to not close the database in the update method. However you should then close it when finished with the database.
Another solution could be to call getWriteableDatabase(); before the query but after the update.
Other solutions could range up to having a singleton thus ensuring that you only have a single instance/connection and then only closing the database when the App is destroyed (I'm using this approach in the app I am using).
P.S. you should also close cursors then they are finished with as they can cause Too many Open errors/issues. Personally I tend to put cursor closing into the activities onDestroy method.
In my app I want to initialize some data stored in a SQLite database.
But it turns out the SQLiteOpenHelper onCreate method is not called when I read my database (it is when I write into my database).
My app crash when I read the db for a table that does not exist (it does not crash if I create the table before). I could catch the exception but does not seem very clean to me.
Is it the normal behavior regarding SQLiteOpenHelper onCreate method or am I missing something?
Here is the initialisation function called in activity OnCreate()
private void InitializeDbPlayerList() {
SQLiteDatabase db;
Cursor cursorPlayers;
DbPlayerData dbPlayerData;
db = mGameDbHelper.getReadableDatabase ();
// The following line crash the app if PLAYER_TABLE_NAME does not exist
cursorPlayers=db.query(GameDatabaseOpenHelper.PLAYER_TABLE_NAME,
GameDatabaseOpenHelper.player_columns, null, new String[] {}, null, null,
null);
cursorPlayers.moveToFirst();
for(int j=0;j<cursorPlayers.getCount();j++)
{
dbPlayerData = new DbPlayerData(cursorPlayers.getString(0),cursorPlayers.getFloat(1),cursorPlayers.getFloat(2),
cursorPlayers.getInt(3),cursorPlayers.getInt(4));
mDbPlayerList .add(dbPlayerData);
}
}
Thx
Fabien
EDIT
First activity opens a database: I used the code
LoginDbAdapter mDbHelper; // as a data member
called
// in my onCreate() of my main activity login
mDbHelper = new LoginDbAdapter(this);
then in my
public void onResume(){
mDbHelper.open(); // opens only when the activity is resumed
super.onResume();
}
then I do the same thing above in my second activity to add a user. This worked.
My issue is as follows:
**How do i open a link to a second table in my database to access
a users information only. And where do i close it. **
UPDATE
an alternative way that works much better is initializing my DbAdapter in the onResume and then calling DbAdapter.open(); only when i need access to the db and closing it right after the work is done with DbAdapter.close();
note: it is also important to call startManagingCursor(cursor); and stopManagingCursor(cursor);
Might these helps:
find these where you getting writeable permission like these::
SQLiteDatabase db=this.getWritableDatabase();
Now wat you need to do iz::
db.insert(TABLE, null, values);
db.close();//put these after inserting your database;
You need to go in to your DATABASEADAPTER class
then close the database connection after insertion as per above code
in your Activity
mDbHelper= new DatabaseAdapter(this);
and in your insert method of DatabaseAdapter class
SQLiteDatabase db = this.getWritableDatabase();
and at last in your insert method call db.close();
You need to Edit these line inside your LoginDbAdapter inside close() method;
public class LoginDbAdapter
{
// close the database
public void close(){
if(mDbHelper != null){
mDbHelper.close();
mDb.close;//insert these line ;these close sqlitedatabase;
}
}
}
give the command to close inside a try{} catch{}
I have a DB that I use in all my activities. There is only one record in the DB.
In the first activity it is opened or created and then put in my globally used object like this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// first get the current data from the DB
myDBAdapter = new MyDBAdapter(this);
GlobalVars.myDBAdapter = myDBAdapter; // we store the DBAdapter in our global var
myDBAdapter.open();
Cursor cursor = myDBAdapter.fetchMainEntry();
startManagingCursor(cursor);
// if there is no DB yet, lets just create one with default data
if (cursor.getCount() == 0) {
createData();
cursor = myDBAdapter.fetchMainEntry();
startManagingCursor(cursor);
}
Now in another activity I access the already open DB like this...
GlobalVars.myDBAdapter.updateMainEntry(1,.....);
I do not close the DB when leavin one activity to go to the next. The DB is just accessed (since it has been opened at the very first activity).
Only when leaving the app I clode the DB like this...
#Override
protected void onDestroy() {
super.onPause();
myDBAdapter.close();
}
The background why I am also asking this is I get this error...
Finalizing cursor android.database.sqlite.SQLiteCursor#48106730 on
mainEntry that has not been deactivated or closed
and it seems that my app crashes on certain devices - but I can't find the reason for it during debugging.
Is that correct and best practice, or do I have to close the DB when I leave the activity and open it when entering the next activity when switching between activities?
Many thanks!
The best thing (I have it tested in a few apps of mine) is to:
declare database adapter as an activity's instance variable:
private DBAdapter mDb;
create it and open in activity's onCreate():
mDb = new DBAdapter(this);
mDb.open();
close it in activity's onDestroy():
mDb.close();
mDb = null;
Works like charm.
A side note: the Application class onTerminate "will never be called on a production Android device" according to the docs.
You can use sqlite as DB for your application. Then you have to create a common class for your whole application Like " DBAdapter ". Then write codes to manipulate the DB. After that you just have to create DBAdapter's object in your activity. Thus you can access your DB from every activity of your app. http://developer.android.com/guide/topics/data/data-storage.html#db This link can be useful.
I've searched all over the net for a solution to this so lets hope someone here can help.
My app has a start up task which populates a SQLite database before loading the main menu. A second activity that can be accessed from the main menu needs access to this. Therefore I close the database in the first activity in order to stop locking errors.
For some reason, the database seems to have no rows as soon as I close the connection, in both the same activity and the second activity.
Heres a code sample:
SQLiteDatabase db = getWritableDatabase(); // get instance of current database
db.beginTransaction(); // set exclusive mode to speed up
for(GulbArticle g : gulbArticles){
this.insert(g);
}
db.setTransactionSuccessful();
// counting here returns 315 rows using the all2() function below
db.close();
// counting here returns 0 rows using the all2() function below
Here is a function I made to get the count back
public void all2(){
SQLiteDatabase db = getReadableDatabase();
String sql = "SELECT COUNT(*) FROM "+TABLE_NAME;
SQLiteStatement statement =db.compileStatement(sql);
long count = statement.simpleQueryForLong();
Log.v("ccount2",count+"");
}
So in both cases I'm initializing an instance of the database, but for some reason as soon as I close it once I can't reopen it/there seems to be nothing in the database. Maybe I'm missing something simple but this has really stumped me.
It seems you forgot to call db.endTransaction();, i.e. to commit the transaction.
It should read:
db.setTransactionSuccessful();
db.endTransaction();
db.close();
Also it is good idea to surround it in try-catch-finaly like this:
try {
db.beginTransaction();
// do your DB manipulation
db.setTransactionSuccessful();
} catch(...) {
...
} finally {
db.endTransaction();
}
db.close();
You don't need to close database when you start second activity. SQLite is normal DB management system - which means that these kind of situation should be resolved.
Resolving of this usually done by transactions mechanism - which you're tried to use in your code.
In your case I believe you have to use non-eclusive transactions - i.e. when record is locked in exclusive mode - record in unaccessible, so basic idiom should looks like:
db.beginTransactionNonExclusive();
try
{
// do smth
db.setTransactionSuccessful();
}
finally
{
db.endTransaction();
}