我的光标只显示从 sqlite 数据库导入的 5 个项目

My cursor shows just 5 items imported from the sqlite database

我正在尝试创建一个从预先填充的数据库中获取数据的测验应用程序。 我使用数据库和 databaseHelperClass。所有获取的数据都加载到模型中 class,问题是数据库中有 15 个元素(最初有 5 个元素,后来我使用 sqlite 浏览器添加了 10 个)但游标只会加载 5 个元素。

我的数据库助手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);
}

型号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();


}

我的数据库: ronaldo table 我的数据库存储在资产文件夹中。

我是 android 开发的新手,非常感谢您的帮助...

只有在数据库不存在的情况下才会应用assets文件夹中的文件。

因此,要从资产文件夹中实施新版本,您需要删除实际数据库。

您可以通过删除应用程序的数据或卸载应用程序并重新运行应用程序来执行此操作。

即整个过程应该是:-

  1. 在 DB Browser 中为 SQLite 修改数据库。
  2. 将assets文件夹中的数据库文件替换为修改后的版本。
  3. 删除应用程序的数据或卸载应用程序。
  4. 重新运行应用程序。