I have the following information that needs to be stored into a database:
id, api_id, api_value1, api_value2, api_value3, api_rating1, api_rating2
From the api, I will either insert the following data into the database:
api_id, api_value1, api_value2, api_value3, api_rating1
OR
api_id, api_value1, api_value2, api_value3, api_rating2
The end result is that some rows will have api_rating1. Some will have api_rating2. Some will have both if the api_id is repeated.
So I have came up with one of 2 implementations:
table 1:
id PRIMARY KEY,
api_id UNIQUE NOT NULL,
api_value1 NOT NULL,
api_value2 NOT NULL,
api_value3 NOT NULL,
api_rating1 UNIQUE,
api_rating2 UNIQUE
//Use this for inserting entry with api_rating1 in android:
db.update(remove api_rating1 from table1 if found)
long id = db.insert(table1);
if(id = -1) {
db.update(table1 with api_values and api_rating1)
}
OR
table1:
id PRIMARY KEY,
api_id NOT NULL,
api_value1 NOT NULL,
api_value2 NOT NULL,
api_value3 NOT NULL,
UNIQUE(api_id) ON CONFLICT REPLACE
table2:
id PRIMARY KEY,
api_rating1 NOT NULL,
table1_id NOT NULL,
UNIQUE(api_rating1) ON CONFLICT REPLACE,
UNIQUE(table1_id) ON CONFLICT REPLACE,
FOREIGN KEY(table1_id) REFERENCES table1(id)
table3:
id PRIMARY KEY,
api_rating2 UNIQUE NOT NULL,
table1_id UNIQUE NOT NULL,
UNIQUE(api_rating2) ON CONFLICT REPLACE,
UNIQUE(table1_id) ON CONFLICT REPLACE,
FOREIGN KEY(table1_id) REFERENCES table1(id)
//Use this for inserting entry with api_rating1 in in android:
long id = db.insert(table1);
long id2 = db.insert(table2 where table1_id = id)
Which one is better implementation? Any suggestions for improvement of the inserting into the tables. I heard that SQLite doesn't have UPSERT like mySQL.
Aucun commentaire:
Enregistrer un commentaire