0

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 ?

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, but if i use AUTOINCREMENT instead of AUTO_INCREMENT then it worked. I faced a new problem with Inserting values to database on Error inserting ... error code: 19 - Contraint failed. May be Only one primary and no UNIQUE another one column can exist in table?
0

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);  
}

1 Comment

Thank you. It OK and as Peshal told me that autoincrement is default.
0

change AUTO_INCREMENT to autoincrement.

2 Comments

Thank you. But Error on inserting value: contraint failed. What is it can be?
I need more infomation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.