terpak
terpak

Reputation: 1151

Android SQLite CREATE TABLE Syntax Error

I'm getting the following error when I execute SQL in my app:

android.database.sqlite.SQLiteException: near "(": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS data (id int auto_increment primary key, data_type int default 0, data float not null, time timestamp default now());

Code causing error:

@Override
public void onCreate(SQLiteDatabase db){
    db.execSQL("CREATE TABLE IF NOT EXISTS " + DATA_TABLE + " (id int auto_increment primary key, data_type int default 0, data float not null, time timestamp default now());");
}

LogCat

12-11 00:13:55.431  22335-22335/com.shockdoc.ama.shockdoc E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.shockdoc.ama.shockdoc, PID: 22335
    android.database.sqlite.SQLiteException: near "(": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS data (_id int auto_increment primary key, data_type int default 0, data float not null, time timestamp default now());
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1788)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1719)
            at com.shockdoc.ama.shockdoc.DB.onCreate(DB.java:57)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
            at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
            at com.shockdoc.ama.shockdoc.DB.saveStat(DB.java:31)
            at com.shockdoc.ama.shockdoc.MainActivity.onSensorChanged(MainActivity.java:99)
            at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:474)
            at android.os.MessageQueue.nativePollOnce(Native Method)
            at android.os.MessageQueue.next(MessageQueue.java:138)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

I've run the line itself through a syntax error checker and it seems to be fine.

Upvotes: 0

Views: 1090

Answers (2)

Kiradev
Kiradev

Reputation: 347

It has to be your table name. "data" is one of many keyword that be using largely on various programing languages, query languages...

So you should try another name. Anyway, "data" doesn't seems to be a meaningful name, programer should make good habit at naming variables...

Upvotes: -1

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

The error is due to default now()

Try using this:

CREATE TABLE IF NOT EXISTS data (id int auto_increment primary key, data_type int default 0, data float not null, time timestamp default current_timestamp);

Check this: SQLite - default a datetime field to the current time (now)

Hope it helps.

Upvotes: 3

Related Questions