如何在Android开放/关闭的SQLite数据库正确正确、数据库、如何在、SQLite

2023-09-07 08:37:30 作者:丶不离开也不想念

我有一个应用程序的正常运行,并且不强制关闭或崩溃。但是,当我看着LogCat中,它有时给我这样的:

  05-20 15:24:55.338:E / SQLiteDatabase(12707):关闭()从来没有明确要求数据库/data/data/com.---.- -  /数据库/ debt.db
05-20 15:24:55.338:E / SQLiteDatabase(12707):android.database.sqlite.DatabaseObjectNotClosedException:应用程序没有关闭在这里打开的游标或数据库对象
 

小的方式下来......

  05-20 15:24:55.338:E /系统(12707):未捕获的异常抛出终结
05-20 15:24:55.338:E /系统(12707):java.lang.IllegalStateException:不具备数据库锁!
 

我不知道,当我要打开和关闭我的数据库?

我有一个主要的活动,是一个简单的启动画面。然后进入调用使用从DB信息一个ListView的活动;所以这是的这个的活动,其中DB第一次打开。

还有之一,在DB需要其他活动,从上述一个具有ListVeew分支。当我应该可以打开和关闭这个?字似乎是,我只需要打开一次,然后关闭,当应用程序被暂停,停止或破坏。

Android开发 SQLiteDatabase的使用一

如果是这样的话,你在哪里我把db.close()方法......在开机画面主要活动,其中的onStop等地处?或相同的酶活性作为打开的DB中的一个?或者..有另一个地方?

更新:

这是该行于code。该错误不断指向:

 公共无效的open()抛出的SQLException {
    数据库= dbHelper.getWritableDatabase();
}
 

解决方案

如果您使用的是DatabaseHelper类的一个实例,在初始化DBHelper对象,每次你做工作,在数据库中,你应该调用open方法之前,你做的工作,然后创建一个新的光标,查询数据库,跟你只是存储在光标的信息,当你完成关闭游标的工作,然后关闭数据库。例如,如果你想抓住每一个项目在一个数据库中,你会做这样的事情:

  ...
DataBaseHelper DB =新DataBaseHelper(本);
...
db.open();
光标光标= db.getAllItems();
MAXCOUNT = cursor.getCount();
随机根=新的随机();
行= gen.nextInt(MAXCOUNT); //生成随机介于0和最大
如果(cursor.moveToPosition(行)){
        串的myString = cursor.getString(1); //这里我想第二列
        displayString(MyString的); //私有方法
    }
cursor.close();
db.close();
 

getAllItems是我DatabaseHelper一个公共方法,它看起来像这样的情况下,你想知道

 公开光标getAllItems()
{
    返回db.query(DATABASE_TABLE,新的String [] {
            KEY_ROWID,
            KEY_NAME
            },
            空值,
            空值,
            空值,
            空值,
            空值);
}
 

这是我如何访问我的数据库,我还没有得到任何你有错误的,它完美的作品。

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.