1

I would like to open a database and get data from it. I read a tutorial about this and made an application. I'm trying to check if the database exists, if not, create a new db in the Android's default system path of your application database and copy the data from a db to there (which is in the assets folder). But when I'm running the application I got the error:

SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase$CursorFactory, String, String[], String) line: 1568   

Can somebody give me some advice about what i made wrong? Here is the code:

package thesis.app.quiz;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelperForQuiz extends SQLiteOpenHelper {

    //The Android's default system path of your application database.
    private static String DB_PATH = "/data/data/thesis.app.quiz/databases/";
    private static String DB_NAME = "QuizDB";
    private SQLiteDatabase myDataBase;
    private final Context myContext;

    public DBHelperForQuiz(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void openDataBase() {


        String myDbPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myDbPath, null,
                SQLiteDatabase.OPEN_READONLY);
    }

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

    public List<Question> getQuestionsList(int difficulty, int numberOfQuestions) {



        List<Question> questionsList = new ArrayList<Question>();

        Cursor c = myDataBase.rawQuery("SELECT * FROM questions", null);

        while (c.moveToNext()) {
            Question q = new Question();
            q.setQuestionText(c.getString(1));
            q.setRightAnswer(c.getString(2));
            q.setAnswerOption1(c.getString(3));
            q.setAnswerOption2(c.getString(4));
            q.setAnswerOption3(c.getString(5));
            q.setQuestionDiffLevel(difficulty);
            questionsList.add(q);
        }

        return questionsList;

    }

    public void createDataBase() throws IOException {

        if (!checkIfDBAlredyExist()) {

            this.getReadableDatabase(); 

            try {
                InputStream myInput = myContext.getAssets().open(DB_NAME);

                String newDbPath = DB_PATH + DB_NAME; 
                OutputStream myOutput = new FileOutputStream(newDbPath);

                byte[] buffer = new byte[1024];
                int length;
                while ((length = myInput.read(buffer)) > 0) {
                    myOutput.write(buffer, 0, length);
                }

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

            } catch (IOException e) {

                e.printStackTrace();
                throw new Error("Error copying database");
            }

        }

    }

    public boolean checkIfDBAlredyExist() {


        SQLiteDatabase tmpDb = null;
        String dbPath = DB_PATH + DB_NAME;
        try {
            tmpDb = SQLiteDatabase.openDatabase(dbPath, null,
                    SQLiteDatabase.OPEN_READONLY);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if (tmpDb != null) {
            tmpDb.close();
            //return true;
        }

        return tmpDb != null ? true : false;
    }
}
3
  • This doesn't look like the actual error - can you post more of the stacktrace? Commented Mar 6, 2012 at 11:56
  • Sorry, for the late answer. I'm saved the LogCat output, and I'm noticed something, so I'm trying to figure out something. Maybe I can solve this problem. I don't want to waste your time. If I will fail again, I will copy the log here. Commented Mar 6, 2012 at 20:49
  • I failed again. I will update the problem. Commented Mar 6, 2012 at 21:14

2 Answers 2

1

You don't need to do that.

The beauty of the SQLiteOpenHelper parent class is that it automatically checks if the database already exists or not.

A call to getReadableDatabase() or getWritableDatabase() will do the following prior to returning a readable or writable SQLiteDatabase object:

  1. If the database does not exist, it will call OnCreate(). This is where you should write code to create your table and initialize if needed.
  2. If the database needs upgrading, the onUpgrade() method will be called. This is where you should do whatever you need to upgrade the table based on how the versions differ(ie. remove/add columns)
Sign up to request clarification or add additional context in comments.

Comments

0

copy this at the class level

private static String ASSETS_DB_FOLDER = "db";

here the db is the object of SQLiteDatabase.

make sure that the name of your database is same as you write in your code(class).

Comments

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.