在某些情况下,当我尝试使用以下代码打开我的 sqlite 数据库时:
dbManager.open();
其中方法如下所示:
SQLiteDatabase mDb;
DbHelper mDbHelper;
public void open(){
mDb=mDbHelper.getWritableDatabase();
}
我收到此错误:
11-08 12:39:25.760: E/SQLiteDatabase(5603): close() was never explicitly called on database '/data/data/act.myactivity/databases/seasonDb'
11-08 12:39:25.760: E/SQLiteDatabase(5603): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
11-08 12:39:25.760: E/SQLiteDatabase(5603): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1980)
11-08 12:39:25.760: E/SQLiteDatabase(5603): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:977)
11-08 12:39:25.760: E/SQLiteDatabase(5603): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
11-08 12:39:25.760: E/SQLiteDatabase(5603): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1021)
11-08 12:39:25.760: E/SQLiteDatabase(5603): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:753)
11-08 12:39:25.760: E/SQLiteDatabase(5603): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
11-08 12:39:25.760: E/SQLiteDatabase(5603): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
11-08 12:39:25.760: E/SQLiteDatabase(5603): at host.database.DB_DatabaseManager.open(DB_DatabaseManager.java:136)
11-08 12:39:25.760: E/SQLiteDatabase(5603): at host.framework.ServicePromemoria.checkUnSyncInsert(ServicePromemoria.java:265)
11-08 12:39:25.760: E/SQLiteDatabase(5603): at host.framework.ServicePromemoria$1.run(ServicePromemoria.java:59)
11-08 12:39:25.760: E/SQLiteDatabase(5603): at java.util.Timer$TimerImpl.run(Timer.java:284)
我认为这与尝试打开尚未关闭的数据库有关。
ps:关闭数据库连接的方法是:
public void close(){
mDb.close();
}
编辑:
这是核心checkUnSyncInsert()
:
dbManager.open(); //here error occurs
String getUserQuery = "SELECT * FROM user";
Cursor c = dbManager.RawQuery(getUserQuery, null);
if(c.getCount() != 0)
{
try{
c.moveToFirst();
usr = c.getString(0).toString();
pwd = c.getString(1).toString();
Log.i("USERNAME: "+pwd,"PASSWORD: "+pwd);
c.close();
}catch(Exception ex)
{
ex.printStackTrace();
c.close();
}
}
else
dbManager.close();
错误发生在第一行 ( dbManager.open()
)