2014-12-11 101 views
0

我收到以下錯誤,當我在我的應用程序執行SQL:的Android SQLite的CREATE TABLE語法錯誤

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

代碼導致錯誤:

@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) 

我已經通過語法錯誤檢查器來運行行本身,它似乎很好。

+0

將代碼添加到出現錯誤(創建表部分)和完整logcat的位置。 – 2014-12-11 05:16:50

回答

3

的錯誤是由於default now()

嘗試使用這樣的:

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

檢查:SQLite - default a datetime field to the current time (now)

希望它能幫助。

-1

它必須是你的表名。 「數據」是許多關鍵字之一,主要用於各種編程語言,查詢語言......

所以你應該嘗試別的名字。無論如何,「數據」似乎並不是一個有意義的名字,程序員應該在命名變量時養成良好的習慣......

+0

[SQLite關鍵字](http://www.sqlite.org/lang_keywords.html) – 2014-12-11 11:39:57