Save new object to table (StorIO)

390 Views Asked by At

I want save all objects ReportDB as new row in DB

 App.get(context)
    .getStorIO()
    .put()
    .object(new ReportDB(keyStore.getKeyCategoryId(), keyStore.getKeyShelfId()))
    .prepare()
    .asRxObservable().subscribe(putResult -> ZLog.d("insertedId = "+putResult.insertedId()),
            throwable -> ZLog.d("throwable = ",throwable));

ReportDB is

@StorIOSQLiteColumn(name = ReportTable.COLUMN_ID,key = true)
long id;

@StorIOSQLiteColumn(name = ReportTable.CATEGORY_ID_I)
long categoryId;

@StorIOSQLiteColumn(name = ReportTable.SHELF_ID_I)
long shelfId;

@StorIOSQLiteColumn(name = ReportTable.LATITUDE_R)
double latitude;

@StorIOSQLiteColumn(name = ReportTable.LONGITUDE_R)
double longitude;

public ReportDB(long categoryId, long shelfId) {
    this.categoryId = categoryId;
    this.shelfId = shelfId;
}

But now, after run code twice, I have only one row and logs

 insertedId = 0
 insertedId = null

How auto increment id ?

1

There are 1 best solutions below

0
On

StorIO does not manage tables schema for you, it just binds to existing db and works with it, so you have 2 ways to achieve autoincrement column:

  1. Create column as INTEGER NOT NULL PRIMARY KEY
  2. Increment counter yourself in the PutResolver of your entity (don't forget about wrapping this code into transaction!)

I'd suggest to use 1st option because it'll work faster and you don't have to write any code for that -> harder to make a mistake :)