jeudi 28 janvier 2016

Android Databse Access Problems

I am currently trying to read data from a SQLite database which is populated outside of the program, however every time i try to access a table which should be within the database i keep getting a table not found error.

Code to get question.

public Question getQuestion(int id,String category) {
   String path = DB_PATH + DB_NAME;
   SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);

    Cursor cursor = database.query(selectTable(category), new String[]
                    {KEY_ID, KEY_QUESTION, KEY_ANSWER1, KEY_IMAGE},
            KEY_ID + "=?" + new String[]{String.valueOf(id)}, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
    }
    Question question = new Question(Integer.parseInt(cursor.getString(0)), cursor.getString(1),
            cursor.getString(2),Uri.parse(cursor.getString(3)));

    database.close();
    cursor.close();
    return question;
}

Copy database from SQL file.

 private void copyDB() 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();

}

Can anyone think of a reason as to why?, i seem to think i am accessing the wrong database however i should only have one database present and if i didn't have a database in the first place i would have a different error.

Any ideas as to whats happening and how to fix it?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire