Mukund
Mukund

Reputation: 1105

Error when creating a table in SQLite database with composite primary key at AUTOINCREMENT

Mycode

    final String createtabBook="CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER AUTOINCREMENT,  bookdir TEXT , lastaddress TEXT,addresname TEXT PRIMARY KEY(bookdir,lastaddress));";
    db.execSQL(createtabBook);

Logcat:

03-05 18:29:31.708: I/System.out(17160): android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error: , while compiling: CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER AUTOINCREMENT,  bookpath TEXT , lastchapter TEXT, PRIMARY KEY(bookpath,lastchapter));

i just want to create a table with composite primary key so that it is a combination of bookdir and lastaddress, also i want to make the lid as auto increment.

Upvotes: 0

Views: 2302

Answers (1)

GrIsHu
GrIsHu

Reputation: 23638

To get the value of any column autoincrement you need to write it or declare it as primary key.

In SQLite a column declared INTEGER PRIMARY KEY will autoincrement.

EDITED:

As you can not define more than one PRIMARY KEY in a table you have to make the bookdir,lastaddress columns as UNIQUE and define the lid columns as PRIMARY KEY as below:

Try out as below:

 final String createtabBook="CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER PRIMARY KEY AUTOINCREMENT,  
  bookdir TEXT , lastaddress TEXT,addresname TEXT, UNIQUE(bookdir,lastaddress));";

Also add "," after the column addresname TEXT, in your query.

Upvotes: 4

Related Questions