0
votes

I am using this simple code to create a table User in my database.

@Override 
public void onCreate(SQLiteDatabase db) {

   String create_t_user = 
                   "CREATE TABLE IF NOT EXISTS user ("+
                   "ID int(10) NOT NULL AUTO_INCREMENT,"+
                   "username varchar(20) NOT NULL,"+
                   "password varchar(20) NOT NULL,"+
                   "connected binary(1) NOT NULL,"+
                   "PRIMARY KEY (ID));";
   db.execSQL(create_t_user);
   // ...

The logcat shows me an error when I try to run that code. It says

"04-28 02:49:57.146: E/AndroidRuntime(10649): android.database.sqlite.SQLiteException: near "AUTO_INCREMENT": syntax error: CREATE TABLE user (ID int(10) NOT NULL AUTO_INCREMENT,username varchar(20) NOT NULL,password varchar(20) NOT NULL,connected binary(1) NOT NULL,PRIMARY KEY (ID))"

2

2 Answers

0
votes

The correct syntax for a rowid column specification using the autoincrement algorithm for generating rowids is

INTEGER PRIMARY KEY AUTOINCREMENT

Note that PRIMARY KEY also implies NOT NULL.

So, change the table creation to

String create_t_user = 
       "CREATE TABLE IF NOT EXISTS user ("+
               "ID INTEGER PRIMARY KEY AUTOINCREMENT,"+
               "username varchar(20) NOT NULL,"+
               "password varchar(20) NOT NULL,"+
               "connected binary(1) NOT NULL);";
0
votes

AUTO_INCREMENT should be AUTOINCREMENT