首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Greendao自动增量插入记录不起作用

Greendao自动增量插入记录不起作用
EN

Stack Overflow用户
提问于 2018-02-26 15:35:50
回答 3查看 1.8K关注 0票数 3

我使用的是greendao 3.2,制作实体和数据库。一切都很好。但我在创建Id属性时遇到了问题,该属性必须为Auto increment。我知道如何在SQL中做到这一点,但是使用greendao会给我带来更多的困难。

我已经声明我的实体是正常的。让我给你举个例子。

代码语言:javascript
复制
@Entity
public class User {

// this will make your id autoincremented
@org.greenrobot.greendao.annotation.Id (autoincrement = true)
private Long Id;
private String name;
@Generated(hash = 690585871)
public User(Long Id, String name) {
    this.Id = Id;
    this.name = name;
}
@Generated(hash = 586692638)
public User() {
}
public Long getId() {
    return this.Id;
}
public void setId(Long Id) {
    this.Id = Id;
}
public String getName() {
    return this.name;
}
public void setName(String name) {
    this.name = name;
}
}

并插入像这样的值

代码语言:javascript
复制
user = new User();
    user.setName("Hello Green Dao");
    Long id = userDao.insertOrReplace(user);

但它的记录一次又一次地重叠,Id为0。它没有像预期的那样工作。

请告诉我原因是什么。我也尝试过使用Insert,但它显示了相同的结果。请帮帮我,我被困住了。

EN

回答 3

Stack Overflow用户

发布于 2018-02-26 15:48:17

我已经使用了它,如下所示,它工作得很好。

代码语言:javascript
复制
@Entity(nameInDb = "cities")
public class City {
  @Id(autoincrement = true)
  private Long id;
  ....
}

唯一的区别是你使用了Id,也许和大写的I一起使用会使它成为保留字,从而导致这个问题。或者,您应该将上面的注释简要介绍给@Id,而不是完整的包路径。我知道这听起来很奇怪,但值得一试。

票数 1
EN

Stack Overflow用户

发布于 2019-06-25 08:56:56

使用Long而不是long。这对我来说很有效

票数 1
EN

Stack Overflow用户

发布于 2018-02-26 15:53:50

我认为你错过了对这个功能的检查。请检查刀

代码语言:javascript
复制
public class UserDao extends AbstractDao<User, Long> {

public static final String TABLENAME = "USER";

/**
 * Properties of entity User.<br/>
 * Can be used for QueryBuilder and for referencing column names.
 */
public static class Properties {
    public final static Property Id = new Property(0, Long.class, "Id", true, "_id");
    public final static Property Name = new Property(1, String.class, "name", false, "NAME");
}


public UserDao(DaoConfig config) {
    super(config);
}

public UserDao(DaoConfig config, DaoSession daoSession) {
    super(config, daoSession);
}

/** Creates the underlying database table. */
public static void createTable(Database db, boolean ifNotExists) {
    String constraint = ifNotExists? "IF NOT EXISTS ": "";
    db.execSQL("CREATE TABLE " + constraint + "\"USER\" (" + //
            "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: Id
            "\"NAME\" TEXT);"); // 1: name
}

/** Drops the underlying database table. */
public static void dropTable(Database db, boolean ifExists) {
    String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"USER\"";
    db.execSQL(sql);
}

@Override
protected final void bindValues(DatabaseStatement stmt, User entity) {
    stmt.clearBindings();

    Long Id = entity.getId();
    if (Id != null) {
        stmt.bindLong(1, Id);
    }

    String name = entity.getName();
    if (name != null) {
        stmt.bindString(2, name);
    }
}

@Override
protected final void bindValues(SQLiteStatement stmt, User entity) {
    stmt.clearBindings();

    Long Id = entity.getId();
    if (Id != null) {
        stmt.bindLong(1, Id);
    }

    String name = entity.getName();
    if (name != null) {
        stmt.bindString(2, name);
    }
}

@Override
public Long readKey(Cursor cursor, int offset) {
    return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
}    

@Override
public User readEntity(Cursor cursor, int offset) {
    User entity = new User( //
        cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // Id
        cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1) // name
    );
    return entity;
}

@Override
public void readEntity(Cursor cursor, User entity, int offset) {
    entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
    entity.setName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
 }

@Override
protected final Long updateKeyAfterInsert(User entity, long rowId) {
    entity.setId(rowId);
    return rowId;
}

@Override
public Long getKey(User entity) {
    if(entity != null) {
        return entity.getId();
    } else {
        return null;
    }
}

@Override
public boolean hasKey(User entity) {
    return entity.getId() != null;
}

@Override
protected final boolean isEntityUpdateable() {
    return true;
}

}

整个项目都被称为here。我想你需要保留一些像这样的支票。在UserDao中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48983527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档