0

I'm trying to create and fetch some data on an Android SQLite database.

When I add the data with create method, objects are correctly updated (the Long id is set). But then, when I fetch the data with queryAll method, objects are correctly returned except the id attribute which comes as null. I'm using DatabaseField tag with generatedId attribute set to true, just like the documentation says:

generatedId

Boolean whether the field is an auto-generated id field. Default is false. Only one field can have this set in a class. This tells the database to auto-generate a corresponding id for every row inserted. When an object with a generated-id is created using the Dao.create() method, the database will generate an id for the row which will be returned and set in the object by the create method. Some databases require sequences for generated ids in which case the sequence name will be auto-generated. To specify the name of the sequence use generatedIdSequence. Only one of this, id, and generatedIdSequence can be specified. See section Fields With generatedId.

There's my class:

@DatabaseTable(tableName = "LANGUAGE")
public class Language {

    @DatabaseField(generatedId = true, columnName = "language_id")
    private Long id;

    @DatabaseField(columnName = "language_locale")
    private String locale;

    @DatabaseField(columnName = "language_active")
    private boolean active;

    @DatabaseField(columnName = "language_name")
    private String name;

    // getters and setters...

}

I'm inserting the data like this:

Language pt = new Language();
pt.setActive(true);
pt.setName("Português");
pt.setLocale("pt_PT");
dao.create(pt);

Language en = new Language();
en.setActive(true);
en.setName("English");
en.setLocale("en_EN");
dao.create(en);

// here, both pt and en have an id, 1 and 2, respectively

But when I execute

dao.queryForAll()

locale, active and name attributes comes ok, but id is null.

Any ideia? I already tried to call commit after create but nothing changes.

Community
  • 1
  • 1
rmpt
  • 606
  • 1
  • 4
  • 24
  • Are you sure your database config file has been fully updated? I've never seen or heard of this, especially with the id field. Does a `dao.queryForId(...)` work but not fill in the id either? – Gray Sep 25 '16 at 00:29
  • queryForId (passing id 1 for instance) returns null. It's very odd, it seems that create method do the job, but then the database stores the entity without id... for my experience, I must have some minor mistake and will only find it through try-error... any help is appreciated – rmpt Sep 25 '16 at 09:01
  • And the ids are assigned correctly/sequentially by create method, so it seems that the entities are in fact on the database. Do you think it can be some cache problem? – rmpt Sep 25 '16 at 09:08
  • This seems like the query process doesn't know about the id file. Do you have an out-of-date `ormlite_config.txt ` file? – Gray Sep 25 '16 at 13:45
  • To be honest I don't have any ormlite_config.txt on my android project. I've added the dependency, created the entities and db helper and it works (except this id problem). Should I create one? – rmpt Sep 25 '16 at 18:14
  • Not unless you want one. I have no idea then why you aren't getting your ids. Are you definitely dealing with the same dao and class/entity when you create as when you query? I'm grasping at straws now. – Gray Sep 26 '16 at 12:28
  • yes, I'm using the same dao. The create and queryForAll code is one right after the other. This is driving me crazy... Already tried clearobjectcache, notify, still nothing... I also tried to add empty entities and then fill them with update method. In this case, all fields come as null, i.e. only the attributes filled at create time are returned in following queries. Seems to me some class/entity configuration, but which? – rmpt Sep 26 '16 at 14:44
  • Sorry, i have no idea. Maybe try clearing out all of your class files and forcing a rebuild? – Gray Sep 26 '16 at 15:02

2 Answers2

1

I had the same problem. I think that it's related to the answer: https://stackoverflow.com/a/34909528

Clearing app data works, but you cannot ask users for it ;)

SQLite has limited ALTER TABLE support that you can use to add a column to the end of a table or to change the name of a table. If you want to make more complex changes in the structure of a table, you will have to recreate the table. You can save existing data to a temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.

ollon
  • 11
  • 1
0

Finally got it to work. Guest what? Just clear data and un-install app and you are ready to go. Don't ask me exactly why, but it must be something to do with the id annotation. At first I was using:

@DatabaseField(id = true, allowGeneratedIdInsert = true, columnName = "language_id")

Giving an error, then I've changed to another combinations and somewhere in the middle the database must be created with some properties. Then, I've change it to the configuration showed in my question, but wasn't working as I explained. After cleaning up the app on my phone, everything had to be created from scratch and the database has finally been created with correct properties.

So, if in the future your database does not stores or load the data correctly, just clear and un-install the app.

halfer
  • 19,824
  • 17
  • 99
  • 186
rmpt
  • 606
  • 1
  • 4
  • 24