hello
hello

Reputation: 33

java.lang.RuntimeException: Unable to start activity

I am getting this error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jitendra.ori/com.example.jitendra.ori.CatalogActivity}: android.database.sqlite.SQLiteException: near "add": syntax error (code 1): , while compiling: CREATE TABLE add (_id INTEGER PRIMARY KEY AUTOINCREMENT, add1 TEXT NOT NULL, add2 TEXT NOT NULL, add3 TEXT NOT NULL, city TEXT, pin INTEGER NOT NULL DEFAULT 0);

My Database create command:

    public void onCreate(SQLiteDatabase db) {

    String SQL_CREATE_PETS_TABLE =  "CREATE TABLE " + AddEntry.TABLE_NAME + " ("
            + AddEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + AddEntry.COLUMN_ADD1 + " TEXT NOT NULL, "
            + AddEntry.COLUMN_ADD2 + " TEXT NOT NULL, "
            + AddEntry.COLUMN_ADD3 + " TEXT NOT NULL, "
            + AddEntry.COLUMN_CITY + " TEXT, "
            + AddEntry.COLUMN_PIN + " INTEGER NOT NULL DEFAULT 0);";

    db.execSQL(SQL_CREATE_PETS_TABLE);
}

Database contract :

    public final static String TABLE_NAME = "add";
    public final static String _ID = BaseColumns._ID;
    public final static String COLUMN_ADD1 ="add1";
    public final static String COLUMN_ADD2 = "add2";
    public final static String COLUMN_ADD3 = "add3";
    public final static String COLUMN_CITY = "city";
    public final static String COLUMN_PIN= "pin";

Please tell what is wrong in this create table command

Upvotes: 1

Views: 125

Answers (4)

ray an
ray an

Reputation: 1288

Keywords are words that have significance in SQL. Certain keywords, such as SELECT, DELETE, or BIGINT, are reserved and require special treatment for use as identifiers such as table and column names. This may also be true for the names of built-in functions.

Nonreserved keywords are permitted as identifiers without quoting. Reserved words are permitted as identifiers if you quote them.

mysql> CREATE TABLE interval (begin INT, end INT);

ERROR 1064 (42000): You have an error in your SQL syntax ... near 'interval (begin INT, end INT)'

BEGIN and END are keywords but not reserved, so their use as identifiers does not require quoting. INTERVAL is a reserved keyword and must be quoted to be used as an identifier:

mysql> CREATE TABLE 'interval' (begin INT, end INT);

Query OK, 0 rows affected (0.01 sec)

ADD is a reserved keyword so you can not use add as a table name. Change the name or quote it.

For additional information and list of other Keywords check this link out. https://dev.mysql.com/doc/refman/5.7/en/keywords.html

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Problem

AddEntry.TABLE_NAME .You should Remove AddEntry . It must be Keyword

Rectify your Query .

String SQL_CREATE_PETS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
            + AddEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," 
            + AddEntry.COLUMN_ADD1 + " TEXT NOT NULL ,"
            + AddEntry.COLUMN_ADD2 + " TEXT NOT NULL ," 
            + AddEntry.COLUMN_ADD3 + " TEXT NOT NULL ,"
            + AddEntry.COLUMN_CITY + " TEXT ," 
            + AddEntry.COLUMN_PIN + " INTEGER NOT NULL DEFAULT 0" + ")";

Clean -Rebuild and Run .

Upvotes: 0

Harsh Singhal
Harsh Singhal

Reputation: 567

Change table name "add" is a keyword in SQLITE

Upvotes: 0

laalto
laalto

Reputation: 152867

add is a sqlite keyword and cannot be used as identifier such as table name without quoting. Better rename the table.

Upvotes: 1

Related Questions