问题
I am trying to insert multiple row data same time to the sqlite database.But for example I have a value.I am getting that from my api.And in this value I have 2000 data.I am just adding 1 row and in that row I Can see 2000 data.
for (NSDictionary *customerDictionary in customerArray) {
Kart *kart = [Kart customerWithName:[customerDictionary valueForKey:@"adi"]];
[_kartList addObject:kart];
}
And I am using FMDB for the sqlite.
EDIT
I can add the one data to the sqlite db with that.But when I try to add another object to the database its adding all datas to the my database in one row.
Its just adding one row and correct data
[database executeUpdate:@"INSERT OR REPLACE INTO KartDB (adi) VALUES (?)" withArgumentsInArray:kart.adi];
Its adding all data from kart.adi and kart.adi2 in one row.
[database executeUpdate:@"INSERT OR REPLACE INTO KartDB (adi,adi2) VALUES (?,?)" withArgumentsInArray:[NSArray arrayWithObjects:kart.adi,kart.adi2, nil]];
Can you give me a suggestion ?
回答1:
What's your PRIMARY KEY
for the table you're trying to add the data into? Perhaps you're simply overriding inserts because the PRIMARY KEY
isn't unique.
Would look something like this.
[database executeUpdate:@"INSERT OR REPLACE INTO KartDB (primaryKey, adi,adi2) VALUES (?,?,?)" withArgumentsInArray:[NSArray arrayWithObjects:primaryKey, kart.adi,kart.adi2, nil]];
Also, if you're doing 2000 inserts at once, look into FMDB's inTransaction
feature. Will make your updates much faster.
You could then call something like:
[dbManager.databaseQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
for (NSDictionary *customerDictionary in customerArray) {
Kart *kart = [Kart customerWithName:[customerDictionary valueForKey:@"adi"]];
[database executeUpdate:@"INSERT OR REPLACE INTO KartDB (primaryKey, adi) VALUES (?,?)", primaryKey, kart.adi];
}
}];
[dbManager.databaseQueue close];
来源:https://stackoverflow.com/questions/33224093/insert-multiple-row-data-same-time-sqlite-with-json-ios