从SD卡获取数据库的问题

时间:2013-03-29 09:24:51

标签: android android-sqlite android-sdcard

我正在尝试读取存储在SDCard上的SQLite数据库。部分代码如下:

public String getTables() {
    String dbFullPath = Environment.getExternalStorageDirectory().toString() + "/external_sd/database.db";
    String query = "SELECT name from sqlite_master where type='table'";
    SQLiteDatabase mDatabase = SQLiteDatabase
            .openDatabase(dbFullPath, null, SQLiteDatabase.OPEN_READONLY
                    + SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    Log.d(TAG, "Is db open? " + mDatabase.isOpen());
    Cursor cursor = mDatabase.rawQuery(query, null);
    String tables = cursor.getString(0);
    cursor.close();

    return tables;
}

但是,运行此代码时,我收到以下错误

  

java.lang.RuntimeException:无法启动活动ComponentInfo {...}:android.database.sqlite.SQLiteException:无法打开数据库文件:,同时编译:来自sqlite_master的SELECT名称,其中type ='table'

Cursor cursor = mDatabase.rawQuery(query, null);行。

调试日志返回Is db open? true,这意味着我的路径正确并且数据库已成功打开?

1 个答案:

答案 0 :(得分:0)

Cursor cursor = mDatabase.query(sqlite_master, new String[] { "name" }, "type=" + table, null,
                null, null, "name asc");