jeudi 31 décembre 2015

android.database.sqlite.SQLiteException: near "update": syntax error (code 1):

My databaseHelper class, which uses 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) 

I tried to create it with commented line but all time got the same error.

I think I forgot an ',' but the statement seems to be ok, I don't know. Thanks for any help.

UPDATE: The error was in KEY_UPDATE and KEY_FROM, just renamed and it works, thanks to laalto.

Aucun commentaire:

Enregistrer un commentaire