I am trying to hardcode a single row into my SQLite database. I want to know if this will work or, if I should do something different? I want to continue using my content values, because I have so much code written with my content values.
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DB_TBL + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_LEVEL + " INTEGER NOT NULL, " +
KEY_HEALTH + " INTEGER NOT NULL, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_CRIT + " INTEGER NOT NULL, " +
KEY_CRIT_RANGE + " INTEGER NOT NULL, " +
KEY_CRIT_INC + " INTEGER NOT NULL, " +
KEY_HIT_RANGE + " INTEGER NOT NULL, " +
KEY_HIT_INC + " INTEGER NOT NULL, " +
KEY_CHAR_IMG + " INTEGER NOT NULL, " +
KEY_TOTAL_XP + " INTEGER NOT NULL, " +
KEY_XP + " INTEGER NOT NULL, " +
KEY_XP_NEEDED + " INTEGER NOT NULL, " +
KEY_COINS + " INTEGER NOT NULL, " +
KEY_SMALL_POTS + " INTEGER NOT NULL, " +
KEY_LARGE_POTS + " INTEGER NOT NULL)"
);
DbHelper helper = new DbHelper();//I have to put a context in the argument
SQLiteDatabase database = helper.getWritableDatabase();//but I don't know what to put or if I'm doing it right
values = new ContentValues();
values.put(KEY_NAME, "User1");
values.put( KEY_LEVEL, 1);
values.put(KEY_HEALTH, 10);
values.put(KEY_CRIT, 5);
values.put(KEY_CRIT_RANGE, 2);
values.put(KEY_CRIT_INC, 2);
values.put(KEY_HIT_RANGE, 2);
values.put(KEY_HIT_INC, 0);
values.put(KEY_TOTAL_XP, 0);
values.put(KEY_XP, 0);
values.put(KEY_XP_NEEDED, 25);
values.put(KEY_COINS, 5);
values.put(KEY_SMALL_POTS, 0);
values.put(KEY_LARGE_POTS, 0);
values.put(KEY_CHAR_IMG, 1);
database.insert(DB_TBL, null, values);
}
logcat
01-04 12:36:06.403: W/dalvikvm(18454): threadid=11: thread exiting with uncaught exception (group=0x40aa3228)
01-04 12:36:06.473: E/AndroidRuntime(18454): FATAL EXCEPTION: UpdateThread
01-04 12:36:06.473: E/AndroidRuntime(18454): java.lang.StackOverflowError
01-04 12:36:06.473: E/AndroidRuntime(18454): at android.database.sqlite.SQLiteOpenHelper.<init>(SQLiteOpenHelper.java:77)
01-04 12:36:06.473: E/AndroidRuntime(18454): at com.tanukiproductions.battleforchristmas.SQLiteTable$DbHelper.<init>(SQLiteTable.java:45)
01-04 12:36:06.473: E/AndroidRuntime(18454): at com.tanukiproductions.battleforchristmas.SQLiteTable$DbHelper.<init>(SQLiteTable.java:46)
That will work, as long as
ourDatabasereferencesdb. But the best way to test it is to try it yourself.As a note, you are putting integers into text columns. While this is not an error in SQLite because of type affinity, it doesn’t really make sense and you might have trouble when you try to sort those columns numerically. You should declare them as
INTEGER.