SQliteDatabase:SQLiteCantOpenDatabaseException错误,请查看

时间:2013-01-03 20:19:55

标签: android sqliteopenhelper sqlite

我正在使用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)

0 个答案:

没有答案