Jochen Birkle
Jochen Birkle

Reputation: 335

Freezing SQLite-DB in Android App

I've run over some difficulties with creating/reading a SQLite-DB in my App.

Having this tutorial here beside me I wanted to implement a Database into my (yeah, laugh at me) Minesweeper-App to store settings like width, height and count of mines in the playfield.

I thought of a table like this:

+-----------+--------+-----+
| Field     | Type   | Key |
+-----------+--------+-----+
| _bez      | text   | pri |
| _x        | int    |     |
| _y        | int    |     |
| _a        | int    |     |
+-----------+--------+-----+

For that I've got this code, I guess, this far all should be fine:

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

This table I want to fill with four standard rows, values for easy, medium, had mode, as well as the current values(which may be changed by the user via three EditText fields in the layout).

Therefore I need methods to add those base values (directly after creating the db), to show the current values to the user and to update the row with current value, when settings are saved.

Here - again based on the tut - I have this:

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

When executing the App it freezes at the point, where data should be read from the db.

I hope this provides enough information that someone may figure out a mistake I made.

Thank you in advance.

Upvotes: 1

Views: 785

Answers (1)

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

Remove SQLiteDatabase db = this.getWritableDatabase(); and db.close(); from set_stdValues() and

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

Upvotes: 2

Related Questions