我在我的应用程序中创建/读取SQLite-DB时遇到了一些困难。
将这个教程here放在我旁边,我想在我的(是的,嘲笑我)Minesweeper-App中实现一个数据库,以存储游戏区域中宽度,高度和地雷数等设置。
我想到了这样一张桌子:
+-----------+--------+-----+
| Field | Type | Key |
+-----------+--------+-----+
| _bez | text | pri |
| _x | int | |
| _y | int | |
| _a | int | |
+-----------+--------+-----+
为此我得到了这段代码,我想,这一切都应该没问题:
public class SettingsDB extends SQLiteOpenHelper {
//DB-Info
private static final int DATENBANK_VERSION = 1;
private static final String DATENBANK_NAME = "minesweeper.db";
private static final String TABLE_SETTINGS = "settings";
//Spalten der Settings-Tabelle
private static final String KEY_BEZ = "bez";
private static final String KEY_X = "x";
private static final String KEY_Y = "y";
private static final String KEY_A = "a";
public SettingsDB(Context context) {
super(context, DATENBANK_NAME, null, DATENBANK_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createDB;
createDB = "CREATE TABLE " + TABLE_SETTINGS + "("
+ KEY_BEZ + " TEXT PRIMARY KEY, "
+ KEY_X + " INTEGER, "
+ KEY_Y + " INTEGER, "
+ KEY_A + " INTEGER)";
db.execSQL(createDB);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SETTINGS);
onCreate(db);
}
}
这个表我想填充四个标准行,easy,medium,have模式的值,以及当前值(可以由用户通过布局中的三个EditText字段更改)。
因此,在保存设置时,我需要方法来添加这些基值(直接在创建数据库之后),向用户显示当前值并使用当前值更新行。
这里 - 再次基于啧啧 - 我有这个:
public void set_stdValues() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues val_easy = new ContentValues();
val_easy.put(KEY_BEZ, "Easy");
val_easy.put(KEY_X, 10);
val_easy.put(KEY_Y, 10);
val_easy.put(KEY_A, 10);
db.insert(TABLE_SETTINGS, null, val_easy);
ContentValues val_curr = new ContentValues();
val_curr.put(KEY_BEZ, "Curr");
val_curr.put(KEY_X, 10);
val_curr.put(KEY_Y, 10);
val_curr.put(KEY_A, 10);
db.insert(TABLE_SETTINGS, null, val_curr);
ContentValues val_med = new ContentValues();
val_med.put(KEY_BEZ, "Med");
val_med.put(KEY_X, 12);
val_med.put(KEY_Y, 12);
val_med.put(KEY_A, 20);
db.insert(TABLE_SETTINGS, null, val_med);
ContentValues val_hard = new ContentValues();
val_hard.put(KEY_BEZ, "Hard");
val_hard.put(KEY_X, 15);
val_hard.put(KEY_Y, 15);
val_hard.put(KEY_A, 40);
db.insert(TABLE_SETTINGS, null, val_hard);
db.close();
}
public Settings getValues(String diff) {
SQLiteDatabase db = this.getReadableDatabase();
String[] cols = new String[] { KEY_BEZ, KEY_X, KEY_Y, KEY_A };
Cursor cursor = db.query(TABLE_SETTINGS, cols, KEY_BEZ + "=?", new String[] { diff }, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
Settings settings = new Settings(cursor.getString(0), cursor.getInt(1), cursor.getInt(2), cursor.getInt(3));
cursor.close();
db.close();
return settings;
}
// public Settings(String bez, int x, int y, int a)
public void setValues(Settings s) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_X, s.get_x());
values.put(KEY_Y, s.get_y());
values.put(KEY_A, s.get_a());
db.update(TABLE_SETTINGS, values, KEY_BEZ + "=?", new String[] { "Curr" });
db.close();
}
执行应用程序时,它会冻结,应该从数据库中读取数据。
我希望这能提供足够的信息,让别人知道我犯的错误。
提前谢谢。
答案 0 :(得分:2)
从SQLiteDatabase db = this.getWritableDatabase();
和
db.close();
和set_stdValues()
@Override
public void onCreate(SQLiteDatabase db) {
String createDB;
createDB = "CREATE TABLE " + TABLE_SETTINGS + "("
+ KEY_BEZ + " TEXT PRIMARY KEY, "
+ KEY_X + " INTEGER, "
+ KEY_Y + " INTEGER, "
+ KEY_A + " INTEGER)";
db.execSQL(createDB);
set_stdValues()
}