【问题标题】:Android sqlite ID not generated未生成 Android sqlite ID
【发布时间】:2015-09-16 10:26:09
【问题描述】:

我目前正在开发一个应用程序,我正在使用数据库。为此,我正在使用 GreenDAO 库。我现在的问题是,当我尝试插入新条目时,GreenDAO 生成的 primaryId 似乎不会自动生成。下面是相关的Codesn-ps:

道生成器

Entity entity = schema.addEntity("Client");
entity.getAdditionalImportsEntity().add("path.to.package.Entity");
entity.setSuperclass("Entity");
entity.addIdProperty().autoincrement();
entity.addBooleanProperty("modified");
entity.addBooleanProperty("inactive");
entity.addStringProperty("hash").index();
...

每个实体对象的保存方法

public void save() {
    if(TextUtils.isEmpty(getHash())) {
        generateHash();
    }
    if(!isAttached()) {
        DatabaseHelper.getSession().insert(this);
    }
    setLinkedHashes();
    modify();
    DatabaseHelper.getSession().update(this);
}

DatabaseHelper.getSession() 返回获取当前的 daoSession,hash-methods 仅在必要时填充/生成一些散列并且并不真正相关。

如果我现在想保存以下实体

this = {path.to.package.dao.Client@831707287832} 
 accuracy = null
 hash = {java.lang.String@831706918568} "6993298135964198b4f135e922e7cd8b"
 id = null
 inactive = null
 lastSeen = null
 modified = null
 nickname = {java.lang.String@831707287888} "Test"
 status = null

我收到以下错误消息

android.database.sqlite.SQLiteConstraintException: CLIENT.CLIENT_ID may not be NULL (code 19)
            at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
            at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
            at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
            at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
            at de.greenrobot.dao.AbstractDao.executeInsert(AbstractDao.java:348)
            at de.greenrobot.dao.AbstractDao.insert(AbstractDao.java:293)
            at de.greenrobot.dao.AbstractDaoSession.insert(AbstractDaoSession.java:63)
            at path.to.package.dao.Entity.save(Entity.java:48)
            at path.to.package.utils.DatabaseHelper.getSelf(DatabaseHelper.java:76)
            at path.to.package.updateService.LocationService$LocationListener.onLocationChanged(LocationService.java:40)
            at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
            at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
            at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

我正在撞墙,找不到我的错误。有人能指出我正确的方向吗?

【问题讨论】:

    标签: android sqlite greendao


    【解决方案1】:

    解决了这个问题:生成时我添加了一个 toMany 关系 - 并将目标实体和源实体混合在一起。因此,客户端实体有第二个 client_id,它既没有使用也没有正确处理。

    【讨论】:

      猜你喜欢
      • 2013-11-18
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 2013-05-19
      • 1970-01-01
      • 2019-07-09
      相关资源
      最近更新 更多