0

我正在尝试将单行硬编码到我的 SQLite 数据库中。我想知道这是否可行,或者我是否应该做一些不同的事情?我想继续使用我的内容值,因为我有很多代码是用我的内容值编写的。

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

    }

日志猫

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)
4

2 回答 2

1

你为什么不试试呢?它应该工作。

于 2013-01-04T16:55:37.570 回答
1

我想知道这是否可行,或者我是否应该做一些不同的事情?

这将起作用,只要ourDatabase引用db. 但最好的测试方法是自己尝试。

请注意,您将整数放入文本列中。虽然这不是 SQLite 中的错误,因为类型 affinity,它并没有真正的意义,当您尝试对这些列进行数字排序时可能会遇到麻烦。您应该将它们声明为INTEGER.

于 2013-01-04T16:55:42.083 回答