anko包数据库相关,createTable扩展方法中AUTOINCREMENT属性编译异常?

创建表的代码如下
 
//wxm:创建表
override fun onCreate(db: SQLiteDatabase) {

//没有使用AUTOINCREMENT的代码运行正常
db.createTable(CityForecastTable.NAME, true,
CityForecastTable.ID to INTEGER + PRIMARY_KEY,
CityForecastTable.CITY to TEXT,
CityForecastTable.COUNTRY to TEXT)

//打印出来的是AUTOINCREMENT的引用:org.jetbrains.anko.db.SqlTypeModifierImpl@759e6bf
logd("AUTOINCREMENT = $AUTOINCREMENT")

// // bug在这段代码产生,AUTOINCREMENT编译成其引用而非其保存的值
// db.createTable(DayForecastTable.NAME, true,
// DayForecastTable.ID to INTEGER + PRIMARY_KEY + AUTOINCREMENT, //wxm:整型,主键,自增长
// DayForecastTable.DATE to INTEGER,
// DayForecastTable.DESCRIPTION to TEXT,
// DayForecastTable.HIGH to INTEGER,
// DayForecastTable.LOW to INTEGER,
// DayForecastTable.ICON_URL to TEXT,
// DayForecastTable.CITY_ID to INTEGER)


//所以,我只能自己拼接sql语句咯,一下代码运行正常
with(DayForecastTable) {
db.execSQL("CREATE TABLE IF NOT EXISTS `" + NAME + "`(" +
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
DATE + " INTEGER, " +
DESCRIPTION + " TEXT, high INTEGER, " +
LOW + " INTEGER, " +
ICON_URL + " TEXT, " +
CITY_ID + " INTEGER);")
}

}

异常信息如下:
 
17948-17978/com.bmbm.weatherdemobykotlin E/SQLiteLog: (1) near "org": syntax error
V/PhoneStatusBarPolicy: updateManagedProfile mManagedProfileFocused: false mKeyguardVisible: false mCurrentUserId:0 mCurrentProfileId:0 mSecondSpaceStatusIconVisible: true showIcon:false
W/Binder_E: type=1400 audit(0.0:980070): avc: denied { ioctl } for path="socket:[50391210]" dev="sockfs" ino=50391210 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
W/Binder_E: type=1400 audit(0.0:980071): avc: denied { ioctl } for path="socket:[50391210]" dev="sockfs" ino=50391210 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
D/ActivityThreadInjector: clearCachedDrawables.
W/System.err: android.database.sqlite.SQLiteException: near "org": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS `DayForecast`(_id INTEGER PRIMARY KEY org.jetbrains.anko.db.SqlTypeModifierImpl@759e6bf, date INTEGER, description TEXT, high INTEGER, low INTEGER, iconUrl TEXT, cityId INTEGER);
W/System.err: at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
W/System.err: at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
W/System.err: at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
W/System.err: at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
W/System.err: at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
W/System.err: at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
W/System.err: at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
W/System.err: at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
W/System.err: at org.jetbrains.anko.db.DatabaseKt.createTable(Database.kt:80)
W/System.err: at com.bmbm.weatherdemobykotlin.data.db.ForecastDbHelper.onCreate(ForecastDbHelper.kt:53)
W/System.err: at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
W/System.err: at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
W/System.err: at org.jetbrains.anko.db.ManagedSQLiteOpenHelper.openDatabase(Database.kt:166)
W/System.err: at org.jetbrains.anko.db.ManagedSQLiteOpenHelper.use(Database.kt:157)
W/System.err: at com.bmbm.weatherdemobykotlin.data.db.ForecastDb.requestForecastByZipCode(ForecastDb.kt:37)
W/System.err: at com.bmbm.weatherdemobykotlin.domain.datasource.ForecastProvider$requestByZipCode$1.invoke(ForecastProvider.kt:26)
W/System.err: at com.bmbm.weatherdemobykotlin.domain.datasource.ForecastProvider$requestByZipCode$1.invoke(ForecastProvider.kt:13)
W/System.err: at com.bmbm.weatherdemobykotlin.domain.datasource.ForecastProvider$requestToSources$1.invoke(ForecastProvider.kt:41)
W/System.err: at com.bmbm.weatherdemobykotlin.domain.datasource.ForecastProvider$requestToSources$1.invoke(ForecastProvider.kt:13)
W/System.err: at com.bmbm.weatherdemobykotlin.extensions.CollectionsExtensionsKt.firstResult(CollectionsExtensions.kt:22)
W/System.err: at com.bmbm.weatherdemobykotlin.domain.datasource.ForecastProvider.requestToSources(ForecastProvider.kt:41)
W/System.err: at com.bmbm.weatherdemobykotlin.domain.datasource.ForecastProvider.requestByZipCode(ForecastProvider.kt:25)
W/System.err: at com.bmbm.weatherdemobykotlin.domain.RequestForecastCommand.execute(RequestForecastCommand.kt:22)
W/System.err: at com.bmbm.weatherdemobykotlin.ui.activitys.MainActivity$loadForecast$1.invoke(MainActivity.kt:44)
W/System.err: at com.bmbm.weatherdemobykotlin.ui.activitys.MainActivity$loadForecast$1.invoke(MainActivity.kt:19)
W/System.err: at org.jetbrains.anko.AsyncKt$doAsync$1.invoke(Async.kt:140)
W/System.err: at org.jetbrains.anko.AsyncKt$doAsync$1.invoke(Async.kt)
W/System.err: at org.jetbrains.anko.AsyncKt$sam$Callable$761a5578.call(Async.kt)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:154)
W/System.err: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)

 我看了一下,发现它编译出来的sql语句是这样子:`DayForecast`(_id INTEGER PRIMARY KEY org.jetbrains.anko.db.SqlTypeModifierImpl@759e6bf, date INTEGER, description TEXT, high INTEGER, low INTEGER, iconUrl TEXT, cityId INTEGER);
 明显是AUTOINCREMENT属性没有编译正确,我也不知道是什么原因
 我是跟着《Kotlin for android developers》这本书敲的代码,作者的代码运行是没问题的,到我这就不行了,郁闷。
 希望大神们能解一解我的困惑,谢谢!
 
 顺便吐槽一下,该网站的编辑器真是闹心,建议添加markdown编辑模式
已邀请:

要回复问题请先登录注册