我是 Sqlite 的初学者,如果我犯了一些愚蠢的错误,我很抱歉:D
我的预定义数据库中出现 NULL 错误:
当我在新的计算机/模拟器中启动我的应用程序时,总是会出现这个错误,所以我认为它与我的方法有关。但是,不应该需要,因为我使用预定义的数据库。onCreate
onCreate
这是我的数据库类:
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);
}
}
任何帮助将不胜感激,谢谢:D