Insert multiple row data same time sqlite with json ios

不羁岁月 提交于 2020-01-16 03:41:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!