I am inserting List<entity>into a database. This object has one field Double. The list contains 49 elements, but when I load it from the database, in + 80% of them, the double value is 0.
Entity Class:
public class NumeroStats implements IStats {
private Long id;
private Integer numero;
private Integer sorties;
private java.util.Date derniereSortie;
private Double percentage;
}
Insert Code:
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=844and always > 0
Before inserting objects, all values are correct. After inserting each NumberStatsand loading it from it id, the values are correct.
But when I load them into one fragment, most of the elements are c percentage=0.
Download code in fragment:
NumeroStatsDao nsDao = mDaoSession.getNumeroStatsDao();
List<NumeroStats> nStatsList = nsDao.queryBuilder().where(NumeroStatsDao.Properties.Numero.notEq(0)).list();
SQLite Editor shows that the values stored in the database with the wrong percentage values, all other fields are correct.
There are no null variables, I double-checked the class NumberStatsDao, and everything looks right (it was generated by the greenDAO generator).
NumberStatsDao:
public class NumeroStatsDao extends AbstractDao<NumeroStats, Long> {
public static final String TABLENAME = "NUMERO_STATS";
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);
}
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 ," +
"'NUMERO' INTEGER," +
"'SORTIES' INTEGER," +
"'DERNIERE_SORTIE' INTEGER," +
"'PERCENTAGE' REAL);");
}
public static void dropTable(SQLiteDatabase db, boolean ifExists) {
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'NUMERO_STATS'";
db.execSQL(sql);
}
@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);
}
}
@Override
public Long readKey(Cursor cursor, int offset) {
return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
}
@Override
public NumeroStats readEntity(Cursor cursor, int offset) {
NumeroStats entity = new NumeroStats(
cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0),
cursor.isNull(offset + 1) ? null : cursor.getInt(offset + 1),
cursor.isNull(offset + 2) ? null : cursor.getInt(offset + 2),
cursor.isNull(offset + 3) ? null : new java.util.Date(cursor.getLong(offset + 3)),
cursor.isNull(offset + 4) ? null : cursor.getDouble(offset + 4)
);
return entity;
}
@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));
}
@Override
protected Long updateKeyAfterInsert(NumeroStats entity, long rowId) {
entity.setId(rowId);
return rowId;
}
@Override
public Long getKey(NumeroStats entity) {
if (entity != null) {
return entity.getId();
} else {
return null;
}
}
@Override
protected boolean isEntityUpdateable() {
return true;
}
}
EDIT: 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 = 0 sorties = 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.
, ? ?