0

我正在使用 SQLiteDatabase,但出现以下错误android.database.sqlite.SQLiteCantOpenDatabaseException。我的活动看起来像这样:我试图在同一个活动中查询数据库两次。而且我每次都分别初始化数据库和游标。

try {
        DBHelper dbHelper = new DBHelper(this.getApplicationContext());
        MyData = dbHelper.getWritableDatabase();
        Cursor c = MyData.rawQuery("SELECT * FROM " + tableName
                + " where Category = '" + category + "'", null);

        results.clear();
        if (c != null) {
            if (c.moveToFirst()) {
                do {
                    String subcat = c.getString(c
                            .getColumnIndex("Subcategory"));

                    results.add(subcat);
                } while (c.moveToNext());
            }
            c.close();
        }
    } catch (SQLiteException se) {
        Log.e(getClass().getSimpleName(),
                "Could not create or Open the database");
    } finally {
        if (MyData != null)
            MyData.execSQL("DELETE FROM " + tableName);
        MyData.close();
    }

我假设错误是由于没有正确关闭数据库。有人可以检查我的代码吗?下面是我的助手类

    public SQLiteDatabase DB;
public String DBPath;
public static String DBName = "sampledatabase";
public static final int version = '1';
public static Context currentContext;
public static String tableName = "MyDatabaseapp";

private static DBHelper mInstance = null;

public DBHelper getInstance(Context activityContext) {

    // Get the application context from the activityContext to prevent leak
    if (mInstance == null) {

        mInstance = new DBHelper(activityContext.getApplicationContext());

    }
    return mInstance;
}

public DBHelper(Context applicationContext) {
    super(applicationContext, DBName, null, version);

    currentContext = applicationContext;
    DBPath = "/data/data/" + applicationContext.getPackageName()
            + "/databases";     
    createDatabase();   

}
@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

private void createDatabase() {
    boolean dbExists = checkDbExists();

    if (dbExists) {
        // do nothing

    } else {
        DB = currentContext.openOrCreateDatabase(DBName, 0, null);
        DB.execSQL("CREATE TABLE IF NOT EXISTS " + tableName
                + " (Category VARCHAR, Subcategory VARCHAR,"
                + " Imageurl VARCHAR, Position INT(3));");

        DB.execSQL("INSERT INTO "
                + tableName
                + " Values ('0','HOT TUB','http://www.massagetherapyreviews.com',1);");
        DB.execSQL("INSERT INTO "
                + tableName
                + " Values ('0','RADIANT','http://www.massagetherapyreviews.com',2);");

        DB.close();
    }

}

private boolean checkDbExists() {
    SQLiteDatabase checkDB = null;

    try {
        String myPath = DBPath + DBName;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);

    } catch (SQLiteException e) {

        // database does't exist yet.
    }

    if (checkDB != null) {
        checkDB.close();
    }

    return checkDB != null ? true : false;
    }

我的 logcat 看起来像这样:

 01-04 01:47:20.572: E/SQLiteDatabase(10274): close() was never explicitly called on database '/data/data/com.afsmedia.android/databases/sample' 
01-04 01:47:20.572: E/SQLiteDatabase(10274): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
01-04 01:47:20.572: E/SQLiteDatabase(10274):    at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:2072)
01-04 01:47:20.572: E/SQLiteDatabase(10274):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1126)
01-04 01:47:20.572: E/SQLiteDatabase(10274):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1083)
01-04 01:47:20.572: E/SQLiteDatabase(10274):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1059)
01-04 01:47:20.572: E/SQLiteDatabase(10274):    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1162)
4

1 回答 1

0

很久以前我也有类似的问题。尝试这个

@Override
protected void onDestroy() {
    super.onDestroy();
    if (c!= null)
        c.close();
    if (dbHelper != null) {
        dbHelper.close();
    }
}
于 2013-01-03T22:11:44.963 回答