0

我在我的应用程序上使用 SQLite,我正在创建一些表,这里是代码:

    db.execSQL("CREATE TABLE IF NOT EXISTS " + GAMES_HISTORY_TABLE+ " (" +
        GAME_KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
        RIGHT_DRILLS + "INTEGER NOT NULL, " + 
        NUM_OF_DRILLS + "INTEGER NOT NULL, " + 
        GAME_DATE + "DATE);"
   );

这是添加记录的代码:

public long addScore(int totalDrills, int numberOfRightDrills) {
    ContentValues cv = new ContentValues();
    cv.put(RIGHT_DRILLS, numberOfRightDrills);
    cv.put(NUM_OF_DRILLS, totalDrills);
    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    cv.put(GAME_DATE, dateFormat.format(date));
    return dataBase.insert(GAMES_HISTORY_TABLE, null, cv);

}

这是常量变量声明(在我的代码开头):

private static final String GAMES_HISTORY_TABLE = "scoers";
private static final String GAME_KEY = "game_id";
private static final String RIGHT_DRILLS = "number_of_rihgt_drill";
private static final String NUM_OF_DRILLS = "total_drill";
private static final String GAME_DATE = "game_date";

我在 logCat 中得到了这个异常:

01-13 20:13:45.485: D/szipinf(658): Initializing inflate state
01-13 20:13:47.375: D/szipinf(658): Initializing inflate state
01-13 20:13:47.485: I/Database(658): sqlite returned: error code = 1, msg = table scoers has no column named game_date
01-13 20:13:47.495: E/Database(658): Error inserting game_date=13-01-2013 20:13:47 total_drill=4 number_of_rihgt_drill=0
01-13 20:13:47.495: E/Database(658): android.database.sqlite.SQLiteException: table scoers has no column named game_date: , while compiling: INSERT INTO scoers(game_date, total_drill, number_of_rihgt_drill) VALUES(?, ?, ?);
01-13 20:13:47.495: E/Database(658):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
01-13 20:13:47.495: E/Database(658):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
01-13 20:13:47.495: E/Database(658):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
01-13 20:13:47.495: E/Database(658):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
01-13 20:13:47.495: E/Database(658):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
01-13 20:13:47.495: E/Database(658):    at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
01-13 20:13:47.495: E/Database(658):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
01-13 20:13:47.495: E/Database(658):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
01-13 20:13:47.495: E/Database(658):    at com.simplemathgame.GameDB.addScore(GameDB.java:83)
01-13 20:13:47.495: E/Database(658):    at com.simplemathgame.Results.onCreate(Results.java:99)
01-13 20:13:47.495: E/Database(658):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-13 20:13:47.495: E/Database(658):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-13 20:13:47.495: E/Database(658):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-13 20:13:47.495: E/Database(658):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-13 20:13:47.495: E/Database(658):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-13 20:13:47.495: E/Database(658):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 20:13:47.495: E/Database(658):    at android.os.Looper.loop(Looper.java:123)
01-13 20:13:47.495: E/Database(658):    at android.app.ActivityThread.main(ActivityThread.java:3683)
01-13 20:13:47.495: E/Database(658):    at java.lang.reflect.Method.invokeNative(Native Method)
01-13 20:13:47.495: E/Database(658):    at java.lang.reflect.Method.invoke(Method.java:507)
01-13 20:13:47.495: E/Database(658):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-13 20:13:47.495: E/Database(658):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-13 20:13:47.495: E/Database(658):    at dalvik.system.NativeStart.main(Native Method)
01-13 20:13:47.495: I/Database(658): sqlite returned: error code = 1, msg = no such column: sent
01-13 20:13:47.495: W/System.err(658): android.database.sqlite.SQLiteException: no such column: sent: , while compiling: SELECT sent FROM feedback
01-13 20:13:47.495: W/System.err(658):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
01-13 20:13:47.506: W/System.err(658):  at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
01-13 20:13:47.506: W/System.err(658):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
01-13 20:13:47.506: W/System.err(658):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
01-13 20:13:47.506: W/System.err(658):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
01-13 20:13:47.506: W/System.err(658):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
01-13 20:13:47.506: W/System.err(658):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
01-13 20:13:47.506: W/System.err(658):  at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1235)
01-13 20:13:47.506: W/System.err(658):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1189)
01-13 20:13:47.506: W/System.err(658):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1271)
01-13 20:13:47.506: W/System.err(658):  at com.simplemathgame.GameDB.isFeedbackGiven(GameDB.java:117)
01-13 20:13:47.506: W/System.err(658):  at com.simplemathgame.Results.onCreate(Results.java:100)
01-13 20:13:47.506: W/System.err(658):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-13 20:13:47.506: W/System.err(658):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-13 20:13:47.506: W/System.err(658):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-13 20:13:47.506: W/System.err(658):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-13 20:13:47.515: W/System.err(658):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-13 20:13:47.515: W/System.err(658):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 20:13:47.515: W/System.err(658):  at android.os.Looper.loop(Looper.java:123)
01-13 20:13:47.515: W/System.err(658):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-13 20:13:47.515: W/System.err(658):  at java.lang.reflect.Method.invokeNative(Native Method)
01-13 20:13:47.515: W/System.err(658):  at java.lang.reflect.Method.invoke(Method.java:507)
01-13 20:13:47.515: W/System.err(658):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-13 20:13:47.515: W/System.err(658):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-13 20:13:47.515: W/System.err(658):  at dalvik.system.NativeStart.main(Native Method)
01-13 20:13:47.525: W/db(658): problem

我试图更改字段的名称和表的名称。我在这个数据库中有另一个表。

我做错什么了?谢谢!

4

2 回答 2

4

您需要在表架构中添加三个空格,然后重新构建它:

db.execSQL("CREATE TABLE IF NOT EXISTS " + GAMES_HISTORY_TABLE+ " (" +
    GAME_KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
    RIGHT_DRILLS + " INTEGER NOT NULL, " +  /* Add a space before "INTEGER... */
    NUM_OF_DRILLS + " INTEGER NOT NULL, " + /* Add a space before "INTEGER... */ 
    GAME_DATE + " DATE);"                   /* Add a space before "DATE... */
);

(仅供参考,GAMES_HISTORY_TABLE显然是"scoers",我认为这是错字。)

于 2013-01-13T20:28:24.123 回答
1

不确定这是否是唯一的错误,但是在创建表时常量之后但在恢复字符串之前缺少空格:

RIGHT_DRILLS + "INTEGER NOT NULL, " + 
NUM_OF_DRILLS + "INTEGER NOT NULL, " + 
GAME_DATE + "DATE);"

应该:

RIGHT_DRILLS + " INTEGER NOT NULL, " + 
NUM_OF_DRILLS + " INTEGER NOT NULL, " + 
GAME_DATE + " DATE);"
于 2013-01-13T20:30:42.807 回答