Reputation: 57
Hello i have json like this, now i want to save this json to sqlite android, but i confused how make a table for this json, the json result is string/text type however we know in create table sqlite we need data with type integer to be primary key.
i want use UserId as primary key but this is type text/string.
My question can i create table with string/text as primary key ? If you have another way to save this json to sqlite, please tell me how to do that. Thanks in advance...
[
{
"UserId": "3c652fc4-5dc4-4fa5-8c07-d27b0b657823",
"Nationality": "ID",
"Image": null,
"IsAlreadyOnTextoaser": true,
"IsAlreadyOnRequest": false,
"LastActive": null,
"LastActiveResume": "0 Days Ago",
"FirstName": "dwi",
"LastName": "wahyudi"
},
{
"UserId": "cbb1f8bd-8016-4bc6-abd1-701fb353293d",
"Nationality": "ID",
"Image": null,
"IsAlreadyOnTextoaser": true,
"IsAlreadyOnRequest": false,
"LastActive": null,
"LastActiveResume": "0 Days Ago",
"FirstName": "Wildan",
"LastName": "Rachman"
}
]
Upvotes: 2
Views: 4489
Reputation: 180080
Many parts of the Android framework expect your data to have a unique integer column called _id
.
(The easiest way to get such a column is to make it an autoincrementing primary key, but but if your data already has such a column, or if a unique value can be easily computed from the other values, you don't need an explicit one.)
While a PRIMARY KEY constraint documents which column identifies rows, the actual technical effects are the same as NOT NULL and UNIQUE. So you can just have multiple candidate keys in your table:
CREATE TABLE MyTable (
_id INTEGER PRIMARY KEY, -- autoincrementing, for Android
UserId TEXT NOT NULL UNIQUE, -- actual PK
FirstName TEXT,
[...]
);
Upvotes: 3