Caused by: android.database.sqlite.SQLiteException: near "org": syntax error (code 1): ,编译时:

Caused by: android.database.sqlite.SQLiteException: near "org": syntax error (code 1): , while compiling:

我正在尝试使用 kotlin 中的 anko 库创建 table,如下所示

override fun onCreate(db: SQLiteDatabase?) {
    db!!.createTable(PersonTable.Name, true,
            Pair(PersonTable.ID, INTEGER + PRIMARY_KEY + AUTOINCREMENT),
            Pair(PersonTable.PersonName, TEXT),
            Pair(PersonTable.Domain, TEXT),
            Pair(PersonTable.MobileNumber, REAL))
}

我在将 AUTOINCREMENT 添加到 ID 时出现以下错误。它在没有 AUTOINCREMENT 的情况下工作。使用 anko 库将任何属性设为 AUTOINCREMENT 的方法是什么。

  E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.williamsro.fourthdemo, PID: 4799
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.williamsro.fourthdemo/com.williamsro.fourthdemo.MainActivity}: android.database.sqlite.SQLiteException: near "org": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS `Person`(id INTEGER PRIMARY KEY org.jetbrains.anko.db.SqlTypeModifierImpl@7d0b905, person_name TEXT, domain TEXT, mobile_number REAL);
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
      at android.app.ActivityThread.-wrap12(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6077)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
   Caused by: android.database.sqlite.SQLiteException: near "org": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS `Person`(id INTEGER PRIMARY KEY org.jetbrains.anko.db.SqlTypeModifierImpl@7d0b905, person_name TEXT, domain TEXT, mobile_number REAL);
      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
      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:1675)
      at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1606)
      at org.jetbrains.anko.db.DatabaseKt.createTable(Database.kt:80)
      at com.williamsro.fourthdemo.AppDbHelpler.onCreate(AppDbHelpler.kt:17)
      at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
      at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
      at org.jetbrains.anko.db.ManagedSQLiteOpenHelper.openDatabase(Database.kt:166)
      at org.jetbrains.anko.db.ManagedSQLiteOpenHelper.use(Database.kt:157)
      at com.williamsro.fourthdemo.MainActivity.insertPerson(MainActivity.kt:44)
      at com.williamsro.fourthdemo.MainActivity.onCreate(MainActivity.kt:72)
      at android.app.Activity.performCreate(Activity.java:6664)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:154) 
      at android.app.ActivityThread.main(ActivityThread.java:6077) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

您使用的 Anko 版本似乎已过时。在版本 0.10 中,连接修饰符的问题已修复,并在版本 0.10.1 中再次恢复。

之前代码的相关部分是 "$modifier $m",其中 $m 是修饰符的字符串表示形式。由于它没有定义 toString() 方法,因此使用实例的 Java 表示。

修复后代码为 "$modifiers ${m.modifier}"。这里的${m.modifier}实际上是一个字符串

如果最新的修复与您无关,请使用版本 0.10

我遇到了同样的问题并找到了解决方法。我能够使用 SqlType.create 函数对 SQL 语句的那部分进行硬编码。

override fun onCreate(db: SQLiteDatabase) {
    db.createTable(CrimeTable.TABLE_NAME, true,
            CrimeTable.ID to SqlType.create("INTEGER PRIMARY KEY AUTOINCREMENT"),
            CrimeTable.UUID to TEXT,
            CrimeTable.TITLE to TEXT,
            CrimeTable.DATE to INTEGER,
            CrimeTable.IS_SOLVED to TEXT
    )
}

我在 DB 浏览器中检查了生成的 SQL,它运行良好。从那以后我再也没有遇到过这个错误。

CREATE TABLE `crimes`(_id INTEGER PRIMARY KEY AUTOINCREMENT, uuid TEXT, title TEXT, date INTEGER, isSolved TEXT)