我正在尝试使用kotlin
中的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
时遇到错误。它没有AUTOINCREMENT
。使用anko
库将任何属性设置为 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)
的方法是什么。
c:\somefolder\someotherfolder\somefile.eml
答案 0 :(得分:2)
您似乎正在使用过时的Anko版本。对于版本 0.10 ,修复了连接修饰符的问题,并再次使用版本 0.10.1 还原。
在代码的相关部分为"$modifier $m"
之前,其中$m
是修饰符的字符串表示形式。由于它没有定义toString()
方法,因此使用了实例的Java表示。
修复代码为"$modifiers ${m.modifier}"
。这里${m.modifier}
实际上是一个字符串。
如果最新修补程序与您无关,请使用版本 0.10 。
答案 1 :(得分:1)
我遇到了同样的问题并找到了解决方法。我能够使用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
)
}
我在数据库浏览器中检查了生成的SQL,它运行正常。从那以后我再也没有犯过这个错误。
CREATE TABLE `crimes`(_id INTEGER PRIMARY KEY AUTOINCREMENT, uuid TEXT, title TEXT, date INTEGER, isSolved TEXT)