简体   繁体   中英

how to add Primary Key in ORMLITE android On Upgrade method?

In my project I have used ORMLite. Now I added a primary key for tje accountId field and also have changed DB Version. Please help me, I don't know how to write query in onUpgrade(...) . For reference I have attached Account class below.

public class Account implements Serializable {
    @DatabaseField
    private String cdate;

    @DatabaseField(id=true)
    private Integer accountId;

    @DatabaseField
    private String Type;

    @DatabaseField
    private String Desc;

    @DatabaseField
    private String UpdatedDate;
}

I don't know how to write query in onUpgrade(...).

The trick is to figure out what Sqlite commands you need to make to add a primary key. You can figure this out by looking at the Sqlite docs for the primary key . There you can see that your accountId field should be defined as:

accountId INTEGER PRIMARY KEY ASC

So then your onUpdate(...) method should be something like:

if (version of db is the old-version) {
    dao.executeRaw("ALTER TABLE account ADD COLUMN accountId INTEGER PRIMARY KEY ASC;");
}

You should do this conditionally based on the old version of the DB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM