ios同时插入多行数据sqlite

ios同时插入多行数据sqlite

本文介绍了使用json ios同时插入多行数据sqlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试同时向 sqlite 数据库中插入多行数据.但例如我有一个值.我是从我的 api 中获取的.在这个值中,我有 2000 个数据.我只是添加了 1 行和在该行中,我可以看到 2000 条数据.

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];
        }

我正在为 sqlite 使用 FMDB.

And I am using FMDB for the sqlite.

编辑

我可以用那个将一个数据添加到 sqlite 数据库中.但是当我尝试将另一个对象添加到数据库时,它会将所有数据添加到我的数据库中.

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];

它将来自kart.adi 和kart.adi2 的所有数据添加到一行中.

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]];

你能给我一个建议吗?

推荐答案

您尝试向其中添加数据的表的 PRIMARY KEY 是什么?也许您只是覆盖了插入,因为 PRIMARY KEY 不是唯一的.

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.

看起来像这样.

[database executeUpdate:@"INSERT OR REPLACE INTO KartDB (primaryKey, adi,adi2) VALUES (?,?,?)" withArgumentsInArray:[NSArray arrayWithObjects:primaryKey, kart.adi,kart.adi2, nil]];

此外,如果您一次进行 2000 次插入,请查看 FMDB 的 inTransaction 功能.将使您的更新更快.

Also, if you're doing 2000 inserts at once, look into FMDB's inTransaction feature. Will make your updates much faster.

你可以这样调用:

[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];

这篇关于使用json ios同时插入多行数据sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 07:09