4

I have sample database with data, I want only to read data.

enter image description here

Thats my DatabaseHelper class

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;
}
}

And in activity class

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

Log cat

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)

I'm also added

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

in AndroidManifest.xml

6
  • possible duplicate of android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database Commented Nov 28, 2013 at 14:07
  • I read that topic, but no effects :( Commented Nov 28, 2013 at 14:09
  • 2
    the issue seems to be related to the fact that you import a database. this database is probably corrupted or malformed. Commented Nov 28, 2013 at 14:11
  • typically, is there an android_metadata table in your db ? Commented Nov 28, 2013 at 14:12
  • 3
    see reigndesign.com/blog/… on preparing your database for integration in android Commented Nov 28, 2013 at 14:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.