Search code examples
androiddatabasesqliteandroid-sqliteandroid-cursor

My cursor shows just 5 items imported from the sqlite database


I am trying to create a quiz app fetching data from a pre-populated database. I use a database and a databaseHelperClass. All the data fetched is loaded into a model class, the problem is there are 15 elements in the database (there were initially 5 elements and later i added 10 using sqlite browser) but the cursor will only load 5 elements.

My database helper class:

public class QuestionsDatabaseHelper extends SQLiteOpenHelper {

String DB_PATH = null;
private static String DB_NAME = "QuizQuestions";
private SQLiteDatabase myDataBase;
private final Context myContext;

public QuestionsDatabaseHelper(Context context) {
    super(context, DB_NAME, null, 10);
    this.myContext = context;
    this.DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
    Log.e("Path 1", DB_PATH);
}


public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if (dbExist) {
    } else {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {
    InputStream myInput = myContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[10];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public void openDataBase() throws SQLException {
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}

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


@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (newVersion > oldVersion)
        try {
            copyDataBase();
        } catch (IOException e) {
            e.printStackTrace();

        }
}

public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
    return myDataBase.query(table, null, null, null, null, null, null);
}

Model class:

public class Questions {

public Questions() {
}

public ArrayList<String> getChoice1() {
    return choice1;
}

public ArrayList<String> getChoice2() {
    return choice2;
}

public ArrayList<String> getChoice3() {
    return choice3;
}

public ArrayList<String> getChoice4() {
    return choice4;
}

public ArrayList<String> getAnswers() {
    return answers;
}

public ArrayList<String> getQuestions() {

    return questions;
}

private Cursor c = null;
private ArrayList<String> choice1 = null;
private ArrayList<String> choice2 = null;
private ArrayList<String> choice3 = null;
private ArrayList<String> choice4 = null;
private ArrayList<String> questions = null;
private ArrayList<String> answers = null;


public void setDataToQuestions(Context context, String player) {
    questions = new ArrayList<>();
    answers = new ArrayList<>();
    choice1 = new ArrayList<>();
    choice2 = new ArrayList<>();
    choice3 = new ArrayList<>();
    choice4 = new ArrayList<>();

    QuestionsDatabaseHelper myDbHelper = new QuestionsDatabaseHelper(context);
    try {
        myDbHelper.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }
    try {
        myDbHelper.openDataBase();
    } catch (SQLException sqle) {
        throw sqle;
    }

    Toast.makeText(context, "Successfully Imported", Toast.LENGTH_SHORT).show();
    c = myDbHelper.query("ronaldo", null, null, null, null, null, null);
    if (c.moveToFirst()) {
        do {

            questions.add(c.getString(1));
            answers.add(c.getString(2));
            choice1.add(c.getString(3));
            choice2.add(c.getString(4));
            choice3.add(c.getString(5));
            choice4.add(c.getString(6));
        } while (c.moveToNext());
    }
            c.close();
    myDbHelper.close();
    Toast.makeText(context, "Successfully Imported" + c.getCount(), Toast.LENGTH_SHORT).show();


}

My database: ronaldo table My database is stored in the assets folder.

I'm a noob in android development and you help will be greatly appreciated...


Solution

  • The file in the assets folder will only be applied if the database doesn't exist.

    So to implement a new version from the assets folder you need to delete the actual database.

    You can do this by deleting the App's data or uninstalling the App and rerunning the App.

    i.e. the entire process should be :-

    1. Amend the database in DB Browser for SQLite.
    2. Replace the database file in the assets folder with the amended version.
    3. Delete the App's Data or uninstall the App.
    4. Rerun the App.