SQLite批量插入IOS代码示例

前端之家收集整理的这篇文章主要介绍了SQLite批量插入IOS代码示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
sqlite批量插入的IOS 代码示例(因为使用的是C API,C/C++用法与之类似):
-------------------------代码的分割线-------------------------
// 打开数据库的部分省略
if (![self beginTransaction]) {
DLog(@ "@H_301_22@开始事务失败 "@H_301_22@);
return;
}
sqlite3_stmt* stmt = NULL;
if (sqlite3_prepare(db,[sql UTF8String],-1,&stmt,NULL) != sqlITE_OK) {
NSLog@H_301_22@(@"数据库prepareStatment失败:%@",sql);
return;
}
for (Record* record in records) {
int bindPos= 1; // 绑定位置从1开始
if (sqlite3_bind_int( stmt@H_301_22@,(bindPos++),[record intValue]) != sqlITE_OK) {
NSLog(@ "@H_301_22@绑定失败:%@ "@H_301_22@,[[NSString alloc] initWithUTF8String:sqlite3_errmsg( db@H_301_22@)]);
continue;
}
if (sqlite3_bind_double( stmt@H_301_22@, (bindPos++),@H_301_22@ [record doubleValue]) != sqlITE_OK) {
NSLog(@ "@H_301_22@绑定失败:%@ "@H_301_22@,[[NSString alloc] initWithUTF8String:sqlite3_errmsg( db@H_301_22@)]);
continue;
}
if (sqlite3_bind_text( stmt@H_301_22@, (bindPos++)@H_301_22@,[ [record stringValue]@H_301_22@UTF8String],sqlITE_STATIC) != sqlITE_OK) {
NSLog(@ "@H_301_22@绑定失败:%@ "@H_301_22@,[[NSString alloc] initWithUTF8String:sqlite3_errmsg( db@H_301_22@)]);
continue;
}
@H_301_22@if (sqlite3_step( stmt@H_301_22@) != sqlITE_DONE) { // 执行插入
NSLog(@ "@H_301_22@%@ "@H_301_22@,[[NSString alloc] initWithUTF8String:sqlite3_errmsg(mDB)]);
continue;
}
sqlite3_reset( stmt@H_301_22@); // 清空绑定数据
}
sqlite3_finalize( stmt@H_301_22@);
if (![self commitTransaction]) {
DLog(@ "@H_301_22@提交事务失败 "@H_301_22@);
return;
}

猜你在找的Sqlite相关文章