【问题标题】:Greendao Autoincrement inserting record not workingGreendao Autoincrement插入记录不起作用
【发布时间】:2018-02-26 07:35:49
【问题描述】:

我正在使用 greendao 3.2 并制作实体和数据库。它一切正常。 但是在创建必须自动递增的 Id 属性时我遇到了麻烦。我知道如何在 SQL 中做到这一点,但使用 greendao 它给了我更多的困难。

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

@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;
}
}

并插入像

这样的值
user = new User();
    user.setName("Hello Green Dao");
    Long id = userDao.insertOrReplace(user);

但它的记录一次又一次地与 ID 0 重叠。它没有按预期工作。

请告诉我是什么原因。我也尝试过使用插入,但它显示相同的结果。请帮助我陷入困境。

【问题讨论】:

    标签: android greendao greendao-generator


    【解决方案1】:

    我已经使用它如下,它工作正常。

    @Entity(nameInDb = "cities")
    public class City {
      @Id(autoincrement = true)
      private Long id;
      ....
    }
    

    唯一的区别是您使用了Id,可能使用大写I 使其成为保留字并导致此问题。或者,也许您应该将其上方的注释简述为 @Id 而不是完整的包路径。我知道这听起来很奇怪,但值得一试。

    【讨论】:

    • 返回0表示没有插入,你有没有试过查询实体,看看有没有插入?
    • 是的,它已插入。例如它插入一条记录,并在插入其他记录时替换第一条
    • @Androidteem 是 Id 的名称归档,你必须全部小写吗?
    • 推荐使用Long
    • @Androidteem 是的,我的意思是字段名称而不是类型。
    【解决方案2】:

    使用 Long 而不是 long 。这对我有用

    【讨论】:

      【解决方案3】:

      我认为您错过了检查此功能。请检查道

      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 中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-07
        • 2012-06-03
        • 2011-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-21
        相关资源
        最近更新 更多