Rameel Hashmi Rameel Hashmi - 2 months ago 6
Android Question

Syntax error sqlite table not created

Here is my code I got this error in logcat.


10-05 17:40:09.650: E/Database(1595): Failure 1 (near ",": syntax error) on 0x8b2d440 when preparing 'CREATE TABLE mytable ( _id INTEGER PRIMARY, name text not null, desig text not null, salary text not null, join text not null, dpay text not null )'.


package example.servant;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class datahandler extends SQLiteOpenHelper {

public static final String KEY_ID="_id";
public static final String KEY_NAME="name";
public static final String KEY_DESIG="desig";
public static final String KEY_SALARY="salary";
public static final String KEY_DPAY="dpay";
public static final String KEY_JOIN="join";

public static final String DATABASE_NAME="servantrecord.sqlite";
public static final String TABLE_NAME="mytable";
public static final String TAG="datahandler";
public static final int DATABASE_VERSION=1;

public static final String DATABASE_CREATE=("CREATE TABLE " + TABLE_NAME + " ( " + KEY_ID
+ " INTEGER PRIMARY, " + KEY_NAME + " text not null, "
+ KEY_DESIG + " text not null, " + KEY_SALARY + " text not null, "
+ KEY_JOIN + " text not null, " + KEY_DPAY + " text not null )");


public datahandler(Context context){

super(context, DATABASE_NAME, null, DATABASE_VERSION);

Log.d("datahandler", "Database created");

}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL(DATABASE_CREATE);
Log.d("datahandler", "table created");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);

}
}

Answer

Replace PRIMARY with PRIMARY KEY.

Change the column join to some other name - join is a reserved SQL keyword.