gpt4 book ai didi

java - Android SQLite CREATE TABLE 语法错误

转载 作者:行者123 更新时间:2023-12-02 05:09:57 24 4
gpt4 key购买 nike

当我在应用程序中执行 SQL 时,出现以下错误:

android.database.sqlite.SQLiteException: 近“(”: 语法错误 (代码 1): ,编译时:CREATE TABLE IF NOT EXISTS data (id int auto_increment 主键,data_type int default 0, data float不为空,时间时间戳默认 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)

我已经通过语法错误检查器运行了该行本身,看起来没问题。

最佳答案

错误是由于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)

希望有帮助。

关于java - Android SQLite CREATE TABLE 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27415805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com