任何机构都可以帮助我吗?我正在做时间表应用程序,所以使用数据库。
我的程序在这里崩溃:
DatabaseHandler dbHelper = new DatabaseHandler(this.getApplicationContext());
SQLiteDatabase db = dbHelper.getReadableDatabase(); //<---------------
Cursor cur=null;
cur = dbHelper.fetchAll(db, "SELECT * FROM timetable");
db.close();
我的 DatabaseHandler 类在这里:
public DatabaseHandler (Context context) {
super(context,DATABASE_NAME, null,DATABASE_VERSION);
}
//Creating Table
@Override
public void onCreate(SQLiteDatabase db){
String CREATE_TABLE = "CREATE TABLE" + TABLE_NAME + "("
+ KEY_ID + "INTEGER PRIMARY KEY," + KEY_SUBJECT_NAME
+ "TEXT," + KEY_SUBJECT_CODE + "TEXT," + KEY_SUBJECT_VENUE
+ "TEXT," + KEY_TIME + "INTEGER," + KEY_DAY + "INTEGER" + ")";
db.execSQL(CREATE_TABLE);
}
//Upgrading Database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
//Insert data
public void insertValues(int day, int time, String name, String code, String venue) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_DAY, day);
values.put(KEY_TIME, time);
values.put(KEY_SUBJECT_NAME, name);
values.put(KEY_SUBJECT_CODE, code);
values.put(KEY_SUBJECT_VENUE, venue);
db.insert(TABLE_NAME, null, values);
//closing the database connection
db.close();
}
public Cursor fetchAll(SQLiteDatabase db, String query){
Cursor cursor = db.rawQuery(query, null);
db.close();
return cursor;
}
}
如果有人可以帮助我,我会非常高兴,因为这是我的最后一年项目:)