ios - 在SQLite3中插入多行

标签 ios xcode sqlite

如何以编程方式将多行插入到 iOS 的 sqlite3 表中?这是我当前方法的代码片段:

sqlite3 *database;

if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
    const char *sqlStatement = "insert into TestTable (id, colorId) VALUES (?, ?)";
    sqlite3_stmt *compiledStatement;

    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
    {
        for (int i = 0; i < colorsArray.count; i++) {
            sqlite3_bind_int(compiledStatement, 1, elementId);
            long element = [[colorsArray objectAtIndex:i] longValue];
            sqlite3_bind_int64(compiledStatement, 2, element);
        }
    }

    if(sqlite3_step(compiledStatement) == SQLITE_DONE) {
        sqlite3_finalize(compiledStatement);
    }
    else {
        NSLog(@"%d",sqlite3_step(compiledStatement));
    }
}
sqlite3_close(database);

这样我只插入第一行,我如何告诉sqlite我希望每个“for”循环都是一个行插入?我找不到任何这样的例子...

谢谢!

最佳答案

您必须运行此语句:

sqlite3_step(compiledStatement) == SQLITE_DONE

每次插入后,在您的代码中我看到您只在最后运行一次。

关于ios - 在SQLite3中插入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13219726/

相关文章:

ios - 负载中未提供 json 请求。 (1269)。试飞

iphone - 如何使用 Objective-C 为两个 View 设置动画

IOS UICollectionView 使用两个 Collection View 时抛出断言

iphone - 如何将数组中的 subview 添加到 uiview 中?

ios - 如何在数组中传递这种格式字典中的数据

ios - Swift 桥接头不导出 React Native 类型

ios - Apple Mach-O 链接器 (id) 警告 : building for MacOSX, 但链接针对为 iOS 构建的 dylib

sqlite - DBD::SQLite HAVING 子句中整数值的占位符

android - 从 SQLite 数据库中删除指定行数

PHP 放弃 SQlite?