简体   繁体   中英

Error when creating a SQLite DB on Android (Code 1)

I am trying to create a simple database but I receive this error:

09-09 12:42:14.750: E/AndroidRuntime(6971): Caused by: android.database.sqlite.SQLiteException: near "values": syntax error (code 1): , while compiling: create table values(_id integer primary key autoincrement, date text not null, screenTime integer not null );

I tried to look at other similar questions without finding a solution although I suspect my create statement to be incorrect but I am not sure. Below is my code.

public class MySQLiteHelper extends SQLiteOpenHelper {

    public static final String TABLE_VALUES = "values";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_DATE = "date";
    public static final String COLUMN_SCREENTIME = "screenTime";

    private static final String DATABASE_NAME = "screenTime.db";
    private static final int DATABASE_VERSION = 1;

    // Database creation sql statement
    private static final String DATABASE_CREATE = "create table " + TABLE_VALUES
          + " ( " + COLUMN_ID + " integer primary key autoincrement, " 
          + COLUMN_DATE + " text not null, "
          + COLUMN_SCREENTIME + " integer not null );";

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

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(MySQLiteHelper.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_VALUES);
        onCreate(db);
    }
} 

VALUES is a keyword .

You could quote it, but it would be a better idea to use a different name. (Note: "values" is pretty much meaningless; every table has values in it.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM