android.database.sqlite.SQLiteException:near“update”:语法错误(代码1):

时间:2015-12-31 13:40:11

标签: android sqlite

我的databaseHelper类,它使用SQLite:

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "AnimeManager";

private static final String TABLE_FAVS = "Animes";


private static final String KEY_TITLE = "title";
private static final String KEY_UPDATE = "update";
private static final String KEY_CAP = "cap";
private static final String KEY_CAP_TOT = "cap_tot";
private static final String KEY_FROM = "from";
private static final String KEY_ALARM = "alarm";


public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
  /*  String CREATE_ANIME_TABLE=" CREATE TABLE "+TABLE_FAVS+" ("
            +KEY_TITLE+" TEXT PRIMARY KEY,"+KEY_UPDATE+" TEXT,"
            +KEY_FROM+" TEXT,"+KEY_CAP+ " INTEGER,"+ KEY_CAP_TOT+" INTEGER,"+KEY_ALARM+" INTEGER)";
   */
    String CREATE_ANIME_TABLE = String.format(
            "CREATE TABLE %s (%s TEXT PRIMARY KEY," +
                    " %s TEXT," +
                    " %s TEXT," +
                    " %s INTEGER," +
                    " %s INTEGER," +
                    " %s INTEGER);", TABLE_FAVS, KEY_TITLE,
            KEY_UPDATE, KEY_FROM, KEY_CAP, KEY_CAP_TOT, KEY_ALARM);
    db.execSQL(CREATE_ANIME_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVS);
    onCreate(db);

}

logcat的:

FATAL EXCEPTION: main
Process: com.vvss.gestoranime, PID: 32580
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vvss.gestoranime/com.vvss.gestoranime.MainActivity}: android.database.sqlite.SQLiteException: near "update": syntax error (code 1): , while compiling: CREATE TABLE Animes (title TEXT PRIMARY KEY, update TEXT, from TEXT, cap INTEGER, cap_tot INTEGER, alarm INTEGER);
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
    at android.app.ActivityThread.access$800(ActivityThread.java:156)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:211)
    at android.app.ActivityThread.main(ActivityThread.java:5373)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: android.database.sqlite.SQLiteException: near "update": syntax error (code 1): , while compiling: CREATE TABLE Animes (title TEXT PRIMARY KEY, update TEXT, from TEXT, cap INTEGER, cap_tot INTEGER, alarm INTEGER);
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:895)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:506)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
    at com.vvss.gestoranime.database.DatabaseHandler.onCreate(DatabaseHandler.java:60)
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
    at com.vvss.gestoranime.database.DatabaseHandler.getFavCount(DatabaseHandler.java:120)
    at com.vvss.gestoranime.MainActivity.onCreate(MainActivity.java:80)
    at android.app.Activity.performCreate(Activity.java:5990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
    at android.app.ActivityThread.access$800(ActivityThread.java:156) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:211) 
    at android.app.ActivityThread.main(ActivityThread.java:5373) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 

我尝试使用注释行创建它,但所有时间都有同样的错误。

我想我忘记了','但声明似乎没问题,我不知道。谢谢你的帮助。

更新:错误发生在KEY_UPDATE和KEY_FROM中,只是重命名并且有效,感谢laalto。

1 个答案:

答案 0 :(得分:2)

updatefromsql keywords。考虑重命名这些列。