2 annbnncnnd AnnBnnCnnD 于 2013.01.14 14:44 提问

android中sqlite一直报错

在应用中用到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

3个回答

abcdeFGh_xyz
abcdeFGh_xyz   2013.01.14 17:26
已采纳

你忘了加空格

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);"
Kill_it
Kill_it   2013.01.14 15:10

在表架构中添加三个空格,然后重建:

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... */
);
zlQQhs
zlQQhs   2013.02.26 00:39

拼接sql语句的时候引用变量的两端要加上空格,再检查你的列名,从日志中看出没有此列名,列名应该错了

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!