greenDAO:实体中的双精度值保存为0

时间:2014-02-26 18:38:58

标签: android greendao

我在数据库中插入List<entity>。该实体有一个Double字段。 List有49个元素,但是当我从数据库加载时,在+ -80%中,double的值为0。

实体类:

public class NumeroStats implements IStats {

    private Long id;
    private Integer numero;
    private Integer sorties;
    private java.util.Date derniereSortie;
    private Double percentage;

    //Ommited gets & sets
}

插入代码:

for (NumeroStats ns : stats.getNumerosStats()) {
    ns.setPercentage((ns.getSorties() * 1.0 / drawCount) * 100);
    Log.d(TAG, "Pre Insert NumeroStats: " + ns.getNumero() + " % " + ns.getPercentage());
    long id = nsDao.insert(ns);
    NumeroStats asd =  nsDao.load(id);
    Log.d(TAG, "Inserted NumeroStats: " + asd.getNumero() + " % " + asd.getPercentage());
}

drawCount=844,始终> 0 在插入实体之前,所有值都是正确的。 插入每个NumberStats并从中加载ID后,值都是正确的。

但是当我将它们加载到一个片段中的大部分项目都带有percentage=0

片段中的加载代码:

NumeroStatsDao nsDao = mDaoSession.getNumeroStatsDao();
List<NumeroStats> nStatsList = nsDao.queryBuilder().where(NumeroStatsDao.Properties.Numero.notEq(0)).list();

SQLite编辑器显示存储在数据库中的值具有错误的百分比值,所有其他字段都是正确的。

没有空变量,我已经仔细检查了NumberStatsDao类,所有看起来都是正确的(它是用greenDAO生成器生成的)。

NumberStatsDao类:

public class NumeroStatsDao extends AbstractDao<NumeroStats, Long> {

    public static final String TABLENAME = "NUMERO_STATS";

    /**
     * Properties of entity NumeroStats.<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 Numero = new Property(1, Integer.class, "numero", false, "NUMERO");
        public final static Property Sorties = new Property(2, Integer.class, "sorties", false, "SORTIES");
        public final static Property DerniereSortie = new Property(3, java.util.Date.class, "derniereSortie", false, "DERNIERE_SORTIE");
        public final static Property Percentage = new Property(4, Double.class, "percentage", false, "PERCENTAGE");
    };


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

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

    /**
     * Creates the underlying database table.
     */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'NUMERO_STATS' (" + //
                "'_id' INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "'NUMERO' INTEGER," + // 1: numero
                "'SORTIES' INTEGER," + // 2: sorties
                "'DERNIERE_SORTIE' INTEGER," + // 3: derniereSortie
                "'PERCENTAGE' REAL);"); // 4: percentage
    }

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

    /**
     * @inheritdoc
     */
    @Override
    protected void bindValues(SQLiteStatement stmt, NumeroStats entity) {
        stmt.clearBindings();

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

        Integer numero = entity.getNumero();
        if (numero != null) {
            stmt.bindLong(2, numero);
        }

        Integer sorties = entity.getSorties();
        if (sorties != null) {
            stmt.bindLong(3, sorties);
        }

        java.util.Date derniereSortie = entity.getDerniereSortie();
        if (derniereSortie != null) {
            stmt.bindLong(4, derniereSortie.getTime());
        }

        Double percentage = entity.getPercentage();
        if (percentage != null) {
            stmt.bindDouble(5, percentage);
        }
    }

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

    /** @inheritdoc */
    @Override
    public NumeroStats readEntity(Cursor cursor, int offset) {
        NumeroStats entity = new NumeroStats( //
                cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
                cursor.isNull(offset + 1) ? null : cursor.getInt(offset + 1), // numero
                cursor.isNull(offset + 2) ? null : cursor.getInt(offset + 2), // sorties
                cursor.isNull(offset + 3) ? null : new java.util.Date(cursor.getLong(offset + 3)), // derniereSortie
                cursor.isNull(offset + 4) ? null : cursor.getDouble(offset + 4) // percentage
        );
        return entity;
    }

    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, NumeroStats entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setNumero(cursor.isNull(offset + 1) ? null : cursor.getInt(offset + 1));
        entity.setSorties(cursor.isNull(offset + 2) ? null : cursor.getInt(offset + 2));
        entity.setDerniereSortie(cursor.isNull(offset + 3) ? null : new java.util.Date(cursor.getLong(offset + 3)));
        entity.setPercentage(cursor.isNull(offset + 4) ? null : cursor.getDouble(offset + 4));
    }

    /**
     * @inheritdoc
     */
    @Override
    protected Long updateKeyAfterInsert(NumeroStats entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }

    /**
     * @inheritdoc
     */
    @Override
    public Long getKey(NumeroStats entity) {
        if (entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /**
     * @inheritdoc
     */
    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }

}

编辑:如果我将百分比设置为1.0,则会发生同样的情况。

ns.setPercentage(1.0);

EDIT2:按以下代码插入实体,所有百分比都与随机值一起存储,始终为!= 0

NumeroStatsDao nsDao = mDaoSession.getNumeroStatsDao();

List<NumeroStats> nsList = new ArrayList<NumeroStats>();

for(int i = 0 ; i <50 ; i++){
    nsList.add(new NumeroStats(null, i+1, i*3,new Date(), new Random().nextDouble()*100));
}

nsDao.insertInTx(nsList);

这看起来很尴尬。我试图在插入循环中插入一个新的NumeroStats实例,但没有成功,很大一部分值仍为0。

nsDao.insert(new NumeroStats (null, ns.getNumero(),ns.getSorties(), ns.getDerniereSortie(), (ns.getSorties() * 1.0 / drawCount) * 100));

编辑3: 我找到了问题的原因,但我找不到合理的解释原因。

在我插入列表NumberStats之前,我插入一个NumberStats numero = 0sorties = 844,即drawcount值。 (我知道这是一个不好的方法。)

像这样:

NumeroStats nsDrawCount = new NumeroStats();
nsDrawCount.setNumero(0);
nsDrawCount.setSorties(drawCount);

nsDao.insert(nsDrawCount);

for (NumeroStats ns : nsList) {
    ns.setPercentage((ns.getSorties() * 1.0 / drawCount) * 100);
    nsDao.insert(ns);
}

如果我没有插入nsDrawCount,则插入循环中的值是正确的。 我已尝试初始化并在循环插入后插入nsDrawCount,之后,出口值也以0值存储。

这很奇怪,不是吗?什么能使它表现得像那样?

0 个答案:

没有答案