0

I'm a beginner in Sqlite, so sorry if I made some stupid mistakes :D

I got a NULL error in my predefined database :

NULL error

This error always happened when I start my App in the new computer/emulator, so I think it has something to do with my onCreate method. But, the onCreate should not needed because I use predefined database.

This is my database class :

private static String DATABASE_NAME = "ALGOLICIOUS_DB";
    private static int DATABASE_VERSION = 1;
    private static String TABLE_NAME = "msQuiz";
    private static String TABLE_HIGHSCORE = "msHighScore";

    private Context context;
    private SQLiteDatabase db;
    String queryCreate, queryUpdate, queryDelete, querySelect, queryInsert;
    Cursor cursor;

    public AlgoliciousDB(Context context) {
        this.context = context; 
        OpenHelper openHelper = new OpenHelper(this.context); 
        this.db = openHelper.getWritableDatabase(); 
    }

    public List<String> getQuestion(int chapter) {
        List<String> questionList = new ArrayList<String>();
        String result = "";
        querySelect = "SELECT * FROM " + TABLE_NAME + " WHERE question_chapter = " + chapter;

        try {
            cursor = this.db.rawQuery(querySelect, null);

            for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
                result += cursor.getString(1) + "%" + cursor.getString(2) + "%" + cursor.getString(3) + "%" + cursor.getString(4) + "%" + cursor.getString(5);
                questionList.add(result);
                result = "";
            }
            return questionList;

        } catch (Exception e) {
            return null;
        }
    }

    public int getHighScore(int chapter) {
        querySelect = "SELECT highscore FROM " + TABLE_HIGHSCORE + " WHERE highscore_id = " + chapter;
        int temp = 0;

        try {
            cursor = this.db.rawQuery(querySelect, null);

            for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
                temp = Integer.parseInt(cursor.getString(0));
            }
            return temp;

        } catch (Exception e) {
            return -1;
        }
    }

    public void Close() {
        db.close();
    }

    class OpenHelper extends SQLiteOpenHelper {
        public OpenHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }

Any help will be appreciated, thanks :D

11
  • 1
    where is your database stored? Commented Dec 3, 2012 at 12:16
  • 1
    when you catch an exception, log it, don't simply return a default value. that should help you pin down the issue Commented Dec 3, 2012 at 12:34
  • 1
    in onCreate, you are supposed to, like, create tables in your database Commented Dec 3, 2012 at 12:35
  • 1
    if you update it, you go in onUpgrade, which drops the table, in which case you'd need to create them again. Also, may be your database copy doesn't work. what do you use to import your DB ? Commented Dec 3, 2012 at 14:18
  • 1
    and still, please do print something when you catch an exception Commented Dec 3, 2012 at 14:18

1 Answer 1

1

Your answer is here: How to use an existing database with an Android application

Copy your database in Assets Folder and use the code provided in above link. Sample sample is also available you can try it.

Sign up to request clarification or add additional context in comments.

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.