Migrating auto generate primary key - android

How can I migrate a primary key field, which was not set to Auto generate before?
From
#PrimaryKey
private int id;
To
#PrimaryKey(autoGenerate=true)
private int id;
Since Sqlite does not support altering columns, my only guess is to migrate the whole table as is and resetting the constraints.
Do I even have to migrate the database during the development process or can I just rebuild it, since my database will change rapidly, so I don't have to migrate every time?

I suggest you to change your approach: add an unique identifier (UID) as alternative way to identify records.
You can define a UID with annotation Entity on your POJO.
#Entity(indices={#Index(value="uid", unique=true)})
publi class Pojo {
..
public String uid;
..
}
When you insert a record in your database, you can define uid field using:
String uuid = UUID.randomUUID().toString();
You can use the UUID field to identify your records, in absolute way. When you migrate to a version to another, you don't work with the old ids, you can always work with UID.

Related

Android Room: auto Increment field

Is there any way to set a field auto increment with android Room?
There is a table which contains 3 fields: id, name, order. And I want the field order to be an auto increment field.
#PrimaryKey(autoGenerate = true)
private long id;
private String name;
private int order;
Set field as primary key can achieve this, but there is already one id.
I can handle the order by myself, maybe set the field order as unique is much safer. But I prefer letting the db do it automatically. How can I do that?
Currently, Android doesn't support auto increment. Even for primary key, its not auto-increment, its auto-generate. It won't be serial numbers. It will generate a random hash numbers.
Auto generate is supported only for primary key but not for any normal column
#PrimaryKey(autoGenerate = true)
Annotate your Entity class with the code above.

No "NOT NULL" and "UNIQUE" constraint on Room Persistence Library

While playing with the Room Persistence Library I came to know that there is no methodology to set a data class field with NOT NULL and also UNIQUE constraints. whether SQLite supports those constraints. Isn't it a problem to migrate old database where those constraints are used? Can anyone give a suggestion for this issue?
I came to know that there is no methodology to set a data class field with NOT NULL and also UNIQUE constraints
A #NonNull annotation on an #Entity field will cause that field's column to have NOT NULL applied to it.
unique=true on an #Index will enforce a uniqueness constraint (e.g., #Entity(indices={#Index(value="something", unique=true)}). However, you are correct that a plain UNIQUE constraint on a column, other than via an index, is not supported.
Isn't it a problem to migrate old database where those constraints are used?
Room is not designed to support existing database structures, particularly in the now-current alpha state. Over time, I would expect Room to support a higher percentage of SQLite features, though I will be stunned if it ever reaches 100%.
Complementary answer about NOT NULL for those using Kotlin:
please note that marking a type as non optional will automatically make it not null (and an optional type will not do that).
You can check it in the schema generated by room with #Database(exportSchema = true) on your database.
For example I have something like that:
#Entity(tableName = "messages")
data class Message (
#PrimaryKey
val messageId: UUID = UUID.randomUUID(),
val date: Date = Date(),
val receivedDate: Date? = null
)
And in the generated schema I can read:
"CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`messageId` TEXT NOT NULL, `date` INTEGER NOT NULL, `receivedDate` INTEGER, PRIMARY KEY(`messageId`))"
(Note: the Date type is here an Int and the UUID a string due to converters I use elsewhere)
If you have multiple item that is to be marked unique & based on that you want to insert in db then you can use composite primary key.
For Not null, Room has provided "#NonNull" annotation that is added over the field that cannot be null.
In the below mentioned eg. roll number is unique in each class but 2 different class can have same roll number. So we can use class & rollNumber as composite primary key & insert in db uniquely.
Example:
#Entity(primaryKeys = {"rollNumber", "class"})
class Student {
#NonNull
private int rollNumber;
private String firstName;
private String lastName;
private int class;
}
for a null able field you can use wrapper primitive type java. for example use Integer instance int in your Room Table.
as in wrapper primitive type java this can bee null but primitive type class cant bee null. and in generation of this SQL code for primitive field that use notNull=true but when use Integer in generayion SQL code use notNull=false

How to design primary key for DynamoDB with 3 attributes

I am new to DDB, but from what I understand, The DynamoDBHashKey is the same as the partition key, and the DynamoDBRangeKey is the same as the sort key. However, I have 3 values that I need to make a unique key. This is for a mobile application. The use case is storing records. See below:
#DynamoDBTable(tableName = "foo")
public class Foo {
private String userID; // Set on a per-device basis by AWS Cognito
private String name; // The user's name. There can be many users on a device
private long time; // The time the record is created
}
So my original though was to make userId the partition/hash key and name the sort/range key, but each of those combinations will have multiple records, thus the addition of the time attribute. Is there a solution here that I am missing?
Here you go..
Parttionkey userid-name
rangekey time
Key point here is that partition key is a concatenated key consisting of two identifiers

Cupboard for SQL-Lite to set unique field on one column

I am using Cupboard for android project. the project was very simple but later I need to set some fields as unique column in my class.
eg:
class book {
Long _id;
String name; // this name should be unique
String auther;
}
I am not able to figure out how to do that.
Issue has been resolved recently by Cupboard:
https://bitbucket.org/littlerobots/cupboard/issues/8/flexible-way-to-control-how-entities-are

Android ORMLite, use pre-populated database and use it

I have a pre-populated database, I hadd .csv and make a database in sqllite manager and imported all values into this database.
Now I put this database into android's assets folder and want to use this via ORMLite in my android application.
Please, need your help and will be thankful to you.
Now I put this database into android's assets folder and want to use this via ORMLite in my android application.
Boy there is a lot of ground to cover here to use ORMLite with this.
The short answer is that you will need to create Java objects which correspond to your database tables. Each Java object should have fields that match the table columns with the appropriate types with #DatabaseField annotations.
For example, if you CSV file was:
# name, id, street
Bill Jones,123,131 Main St.
and your table created is something like:
create table user (name VARCHAR(255), integer id, street VARCHAR(255));
The Java object you will need is something like:
public class User {
#DatabaseField(id = true)
int id;
#DatabaseField
String name;
#DatabaseField
String street;
}
Then you would use ORMLite to read in objects from your database. You should see the ORMLite home page and the Getting Started section of the documentation. For linking up with the existing database, you should read the section of the manual about using with Android.
Any additional questions I'd ask to the ORMLite Users Mailing List.

Categories

Resources