问题
I'm trying to get the questions from the SqLite
for the quiz. I am getting unknowing error. I'm not understanding how to solve that error and how I got that. I'm getting stuck in this since many days. go through many docs but it is not helping to solve the error.
This is my DBHelper:
public class DBHelper extends SQLiteOpenHelper {
private String packageName;
private static final String db_name = "quiz_db.db";
private String db_path;
private static int db_version = 1;
Context con;
public DBHelper(Context con) {
super(con, db_name, null, db_version);
// TODO Auto-generated constructor stub
this.con = con;
db_path = con.getDatabasePath(db_name).toString().replace(db_name, "");
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
public void createDB() throws IOException {
if (checkDB()) {
} else if (!checkDB()) {
this.getReadableDatabase();
copyDB();
}
}
private boolean checkDB() {
SQLiteDatabase cDB = null;
try {
cDB = SQLiteDatabase.openDatabase(db_path + db_name, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
e.printStackTrace();
}
if (cDB != null) {
cDB.close();
}
return cDB != null ? true : false;
}
private void copyDB() throws IOException {
InputStream inputFile = con.getAssets().open(db_name);
String outFileName = db_path + db_name;
OutputStream outFile = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = inputFile.read(buffer)) > 0) {
outFile.write(buffer, 0, length);
}
outFile.flush();
outFile.close();
inputFile.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public List<Quizplay> getQuestionGuj(int noOfQuestion, int level) {
List<Quizplay> quizplay = new ArrayList<Quizplay>();
int total = noOfQuestion;
String sql = "select * FROM questions_list where level=" + level + " ORDER BY RANDOM() LIMIT " + total;
SQLiteDatabase db = this.getReadableDatabase();
//SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/" + packageName + "/databases/" + DATABASE_NAME, null, 0);
Cursor cursor = db.rawQuery(sql, null);
int i = 1;
if (cursor.moveToFirst()) {
do {
Quizplay question = new Quizplay(cursor.getString(cursor.getColumnIndex("question")));
question.addOption(cursor.getString(cursor.getColumnIndex("option_a")));
question.addOption(cursor.getString(cursor.getColumnIndex("option_b")));
question.addOption(cursor.getString(cursor.getColumnIndex("option_c")));
question.addOption(cursor.getString(cursor.getColumnIndex("option_d")));
String rightAns = cursor.getString(cursor.getColumnIndex("right_answer"));
System.out.println("right ans "+rightAns);
if (rightAns.equalsIgnoreCase("A")) {
question.setTrueAns(cursor.getString(cursor.getColumnIndex("option_a")));
} (...)
if (question.getOptions().size() == 4) {
quizplay.add(question);
}
i++;
} while (cursor.moveToNext());
}
cursor.close();
db.close();
Collections.shuffle(quizplay);
quizplay = quizplay.subList(0, noOfQuestion);
return quizplay;
}
}
thank you so so very much in advance.
来源:https://stackoverflow.com/questions/61954232/sqlite-error-when-compiling-when-fetching-the-data-from-sqlite