无法打开SQLite数据库

时间:2013-11-28 14:05:52

标签: android sqlite

我有数据的示例数据库,我只想读取数据。

enter image description here

这就是我的DatabaseHelper类

private static String DB_PATH = "/data/data/com.blue/databases/";

private static String DB_NAME = "QuestionDB";
private SQLiteDatabase myDatabase;
private final Context myContext;

public DatabaseHelper(Context context) {
    super(context, DB_NAME, null, 2);
    myContext = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    if (!databaseExist()) {
        this.getReadableDatabase();
        try {
            copyDatabase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

private boolean databaseExist() {
    boolean checkdb = false;
    try {
        String myPath = myContext.getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator + DB_NAME;
        File dbfile = new File(myPath);
        checkdb = dbfile.exists();
    } catch (SQLiteException e) {
        System.out.println("Database doesn't exist");
    }
    return checkdb;
}

private void copyDatabase() throws IOException {
    InputStream myInput = myContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024];
    int length;

    while ((length = myInput.read(buffer)) > 0)
        myOutput.write(buffer, 0, length);

    myOutput.flush();
    myOutput.close();
    myInput.close();
}

public void openDatabase() throws SQLException {
    String myPath = DB_PATH + DB_NAME;
    /* THAT LINE CAUSE PROBLEMS */myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}

@Override
public synchronized void close() {
    if (myDatabase != null)
        myDatabase.close();
    super.close();
}

public String getTextQuestion(int id) {
    myDatabase = this.getReadableDatabase();
    Cursor cursor = myDatabase.rawQuery("SELECT question FROM QuestionDB WHERE id = " + id, null);
    if (cursor != null)
        cursor.moveToFirst();
    String contact = cursor.getString(0);
    cursor.close();
    return contact;
}
}

在活动课

DatabaseHelper myDbHelper = new DatabaseHelper(this);
myDbHelper.openDatabase();
Toast.makeText(getApplicationContext(), "Test: " + myDbHelper.getTextQuestion(1), Toast.LENGTH_LONG).show();

记录猫

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blue/com.blue.screen.MainMenu}: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
E/AndroidRuntime(5771): Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
E/AndroidRuntime(5771):     at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
E/AndroidRuntime(5771):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
E/AndroidRuntime(5771):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
E/AndroidRuntime(5771):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
E/AndroidRuntime(5771):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
E/AndroidRuntime(5771):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
E/AndroidRuntime(5771):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
E/AndroidRuntime(5771):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
E/AndroidRuntime(5771):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
E/AndroidRuntime(5771):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)

我也加了

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

AndroidManifest.xml中的

0 个答案:

没有答案