1

我在我的应用程序中创建/读取 SQLite-DB 时遇到了一些困难。

有了这个教程我想在我的(是的,嘲笑我)扫雷应用程序中实现一个数据库,以存储诸如宽度、高度和操场中地雷数量之类的设置。

我想到了一个这样的表:

+-----------+--------+-----+
| 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);
}
}

我想用四个标准行填充这个表,简单、中等、有模式的值,以及当前值(用户可以通过布局中的三个 EditText 字段更改)。

因此,我需要添加这些基值的方法(直接在创建数据库之后),向用户显示当前值并在保存设置时用当前值更新行。

在这里 - 再次基于 tut - 我有这个:

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();
    }

执行应用程序时,它会冻结在应该从数据库中读取数据的位置。

我希望这提供了足够的信息,有人可能会发现我犯的错误。

先感谢您。

4

1 回答 1

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()
}
于 2013-03-30T01:12:53.540 回答