任何機構都可以幫助我。我正在做時間表應用程序,所以使用數據庫。Android Sqlite getReadableDatabase
我的程序崩潰的位置:
DatabaseHandler dbHelper = new DatabaseHandler(this.getApplicationContext());
SQLiteDatabase db = dbHelper.getReadableDatabase(); //<---------------
Cursor cur=null;
cur = dbHelper.fetchAll(db, "SELECT * FROM timetable");
db.close();
我的數據庫處理器類是在這裏:
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;
}
}
我會更樂意如果有人可以幫助我,因爲這是我的最後一年的項目:)
我只需要獲取數據庫中的所有數據 – Alibek
這很簡單,只需比較你的代碼:http://dj-android.blogspot.in/2011/11/sqlite-database-with-cursor-adapter-in .html&http://www.vogella.com/articles/AndroidSQLite/article.html&http://dj-android.blogspot.in/2012/10/android-show-data-from-sqlite-db-into .html,網上有這麼多的教程。 –
這將有助於*不*保留錯誤信息的祕密。 –