我有一个在同一个数据库中使用2个表的应用程序。我在辅助类中创建并预填充这些表,如下所示。
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE [weekmenu] ([weekmenuID] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, [receptenLijst] VARCHAR(50) NOT NULL )";
db.execSQL(sql);
sql = "INSERT INTO weekmenu (receptenLijst) values('0')"; db.execSQL(sql);
sql = "CREATE TABLE [recepten] ([receptID] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, [receptNaam] TEXT NOT NULL UNIQUE, [receptURL] TEXT )";
db.execSQL(sql);
sql = "INSERT INTO recepten (receptNaam, receptURL) values('Macaroni met spek en broccoli', 'http://www.kooknet.be/recepten/hoofdgerecht/macaroni-met-spek-en-broccoli')"; db.execSQL(sql);
sql = "INSERT INTO recepten (receptNaam, receptURL) values('Broccolipuree met spekblokjes en fishsticks', 'http://www.kooknet.be/recepten/hoofdgerecht/broccolipuree-met-spekblokjes-en-fishsticks')"; db.execSQL(sql);
sql = "INSERT INTO recepten (receptNaam, receptURL) values('Hoeveomelet', 'http://www.kooknet.be/recepten/hoofdgerecht/hoeveomelet')"; db.execSQL(sql);
sql = "INSERT INTO recepten (receptNaam, receptURL) values('Gevulde courgettes', 'http://www.kooknet.be/recepten/hoofdgerecht/gevulde-courgettes')"; db.execSQL(sql);
}
在另一项活动中,我想确保用户可以在按下按钮时更新表格周菜单。每次用户点击按钮时,周围菜单中的相同记录应该被覆盖(通过更新表格)
btnWeekmenuOpslaan.setOnClickListener(new View.OnClickListener() {
// Database aanroepen
SQLiteDatabase db = oData.getReadableDatabase();
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
String receptenLijst =
txtMaandagReceptID.getText().toString() + "-" +
txtDinsdagReceptID.getText().toString() + "-" +
txtWoensdagReceptID.getText().toString() + "-" +
txtDonderdagReceptID.getText().toString() + "-" +
txtVrijdagReceptID.getText().toString() + "-" +
txtZaterdagReceptID.getText().toString() + "-" +
txtZondagReceptID.getText().toString();
// database updaten
values.put("receptenLijst", receptenLijst);
db.update("weekmenu", values, "weekmenuID=?",new String[] {Long.toString(1)});
db.close();
oData.close();
}
});
不幸的是,活动停止,在调试模式下,我收到“周菜单:没有这样的表存在”的消息。
有人能帮帮我吗?我一直在阅读Stackoverflow上的很多帖子,但没有一个能帮助我。
谢谢!
答案 0 :(得分:0)
你的罪魁祸首在这里:
SQLiteDatabase db = oData.getReadableDatabase();
您需要一个可写数据库。不可读。
将其更改为:
QLiteDatabase db = oData.getWritableDatabase();