Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(1) near "AUTO_INCREMENT": syntax error when altering table

Tags:

android

sqlite

I'm trying to alter an SQLite table defined as follows:

    String CREATE_TABLE="CREATE TABLE IF NOT EXISTS " + tab_NAME + " ("
                            + row_ID + " INTEGER PRIMARY KEY , "
                            + row_NAME + " TEXT, "
                            + row_EMAIL + " TEXT, "
                            + row_WEBSITE + " TEXT, "
                            + row_TELEPHONE1 + " TEXT, "
                            + row_TELEPHONE2 + " TEXT, "
                            + row_TELEPHONE3 + " TEXT, "
                            + row_TELEPHONE4 + " TEXT, "
                            + row_TELEPHONE5 + " TEXT);";
        db_name.execSQL(CREATE_TABLE);

        String newTab="CREATE TABLE IF NOT EXISTS temp ("
                + row_ID + " INTEGER PRIMARY KEY AUTOINCREMENT , "
                + row_NAME + " TEXT, "
                + row_EMAIL + " TEXT, "
                + row_WEBSITE + " TEXT, "
                + row_TELEPHONE1 + " TEXT, "
                + row_TELEPHONE2 + " TEXT, "
                + row_TELEPHONE3 + " TEXT, "
                + row_TELEPHONE4 + " TEXT, "
                + row_TELEPHONE5 + " TEXT);";
        db_name.execSQL(newTab);

And here is the auto_increment alter statement:

    String alter="ALTER TABLE temp AUTO_INCREMENT=1;";
    ourDatabase.execSQL(alter);

I got an error saying (1) near AUTO_INCREMENT: syntax error

what's wrong?? any help??

like image 503
Ahlam Avatar asked Dec 23 '12 14:12

Ahlam


2 Answers

In SQLite a column declared INTEGER PRIMARY KEY will autoincrement by itself. Remove the AUTOINCREMENT keyword and then try.

Check the SQLite FAQ

like image 197
Abhishek Sabbarwal Avatar answered Sep 18 '22 14:09

Abhishek Sabbarwal


In sqlite setting column as PRIMARY KEY is an AUTOINCREMENT. So there is no need of specifying explicitly column as AUTOINCREMENT. Check this link for your reference.

like image 39
arthankamal Avatar answered Sep 22 '22 14:09

arthankamal