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

AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
Blaze Tama
  • 10,828
  • 13
  • 69
  • 129

1 Answers1

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.

Community
  • 1
  • 1
Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149