• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps SQLite database help

Gewin

Lurker
Hi!
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:
Code:
SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase$CursorFactory, String, String[], String) line: 1568
The LogCat say's there is no table with the name of "QUESTIONS". So I'm checked that the database exist. I used DDMS file explorer, and I saw the database exist. I'm pulled the database from the "emulator" and opened it with SQLite Database Browser. I'm noticed, that the question table Isn't exist in the copied database, just in the database in the assets folder. (ps: In the "right" database ( in the assets folder ) I've got the table android_metadata and in the questions table I've got the field _id just as the tutorials said).
Can anybody help me?

Here is my code and logcat log:
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;
    }
}
And here is LogCat log:
Code:
03-06 21:41:08.904: D/gralloc_goldfish(556): Emulator without GPU emulation detected.
03-06 21:41:34.154: I/SqliteDatabaseCpp(556): sqlite returned: error code = 1, msg = no such table: QUESTIONS, db=/data/data/thesis.app.quiz/databases/QuizDB
03-06 21:41:34.164: D/AndroidRuntime(556): Shutting down VM
03-06 21:41:34.164: W/dalvikvm(556): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
03-06 21:41:34.204: E/AndroidRuntime(556): FATAL EXCEPTION: main
03-06 21:41:34.204: E/AndroidRuntime(556): android.database.sqlite.SQLiteException: no such table: QUESTIONS: , while compiling: SELECT * FROM QUESTIONS WHERE DIFFICULTY=2 ORDER BY RANDOM() LIMIT 10
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1538)
03-06 21:41:34.204: E/AndroidRuntime(556):  at thesis.app.quiz.DBHelperForQuiz.getQuestionsList(DBHelperForQuiz.java:73)
03-06 21:41:34.204: E/AndroidRuntime(556):  at thesis.app.quiz.QuizActivityMain.getQuestionsFromDB(QuizActivityMain.java:99)
03-06 21:41:34.204: E/AndroidRuntime(556):  at thesis.app.quiz.QuizActivityMain.onClick(QuizActivityMain.java:50)
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.view.View.performClick(View.java:3511)
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.view.View$PerformClick.run(View.java:14105)
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.os.Handler.handleCallback(Handler.java:605)
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.os.Looper.loop(Looper.java:137)
03-06 21:41:34.204: E/AndroidRuntime(556):  at android.app.ActivityThread.main(ActivityThread.java:4424)
03-06 21:41:34.204: E/AndroidRuntime(556):  at java.lang.reflect.Method.invokeNative(Native Method)
03-06 21:41:34.204: E/AndroidRuntime(556):  at java.lang.reflect.Method.invoke(Method.java:511)
03-06 21:41:34.204: E/AndroidRuntime(556):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-06 21:41:34.204: E/AndroidRuntime(556):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-06 21:41:34.204: E/AndroidRuntime(556):  at dalvik.system.NativeStart.main(Native Method)
03-06 21:41:35.994: I/Process(556): Sending signal. PID: 556 SIG: 9
 
Sounds like it isn't copying the database from the assets folder to the data folder. Does the database in the assets folder have a file extension? You are showing it named as QuizDB but it should be something like QuizDB.db or QuizDB.sqlite.
 
Sounds like it isn't copying the database from the assets folder to the data folder. Does the database in the assets folder have a file extension? You are showing it named as QuizDB but it should be something like QuizDB.db or QuizDB.sqlite.
No, it hasn't. I took care of that. It's name is just QuizDB with no file extension.
 
I'd delete the database in the data folder and put a break point in your try/catch statement where it is copying the file from the assets folder. Then run it in debug mode and step through those statements to make sure it is actually copying the file.
 
I'd delete the database in the data folder and put a break point in your try/catch statement where it is copying the file from the assets folder. Then run it in debug mode and step through those statements to make sure it is actually copying the file.
Thanks for the advice, i will try it tomorrow!
 
Back
Top Bottom