user2553773
user2553773

Reputation: 1

How to create a query string using SQLiteOpenHelper class?

I want to create a table with an ID and an UNIQUE TEXT field such using:

@Override   
public void onCreate(SQLiteDatabase db) 
{   
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_POSTS + "("
    + KEY_ID + " INTEGER PRIMARY KEY AUTO_INCREMENT," + KEY_URL + " TEXT UNIQUE NOT NULL"+")";
    db.execSQL(CREATE_CONTACTS_TABLE);  
}

But this isn't working.

07-23 10:35:53.937: E/AndroidRuntime(19078): android.database.sqlite.SQLiteException: near "AUTO_INCREMENT": syntax error: CREATE TABLE posts(id INTEGER PRIMARY KEY AUTO_INCREMENT,url TEXT UNIQUE NOT NULL)

How can I fix this ?

Upvotes: 0

Views: 560

Answers (3)

outcast
outcast

Reputation: 568

change AUTO_INCREMENT to autoincrement.

Upvotes: 0

ObieMD5
ObieMD5

Reputation: 2657

This will work to create the table, I tested it within my application and worked fine.

@Override   
public void onCreate(SQLiteDatabase db) 
{   
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_POSTS + " ("
    + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_URL + " TEXT UNIQUE NOT NULL"+")";
    db.execSQL(CREATE_CONTACTS_TABLE);  
}

Upvotes: 0

Peshal
Peshal

Reputation: 1518

The Integer primary key in SQlite is AutoIncrement by default so remove the Auto Increment. Here is the documentation SQLite FAQ

Upvotes: 1

Related Questions