objective-c - 使用数组添加批处理值

标签 objective-c sqlite

我正在尝试将数组中的批处理数据插入到我的移动项目的 Sqlite3 中。

我的代码似乎工作正常,但它并没有插入所有数据。

PS:我编辑了我的代码:

        -(BOOL) saveBatchData:(NSArray *)userSourceID userDestID:(NSArray *)userDestID message:(NSArray *)message sentDate:(NSArray *)sentDate
{
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        for(int i=0; i<[message count]; i++)
        {

            NSString *insertSQL = [NSString stringWithFormat:@"insert into messages (userSourceID,userDestID, message, sentDate) values (\"%d\",\"%d\", \"%@\",\"%@\")",[[userSourceID objectAtIndex:i] integerValue],[[userDestID objectAtIndex:i] intValue], [message objectAtIndex:i],[sentDate objectAtIndex:i]];

            const char *insert_stmt = [insertSQL UTF8String];

            sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);

            if (sqlite3_step(statement) == SQLITE_DONE)
            {
                sqlite3_finalize(statement);

                return YES;

            } else {

                NSLog(@"Hata = %s",sqlite3_errmsg(database));

                return NO;
            }

            sqlite3_reset(statement);

        }

        sqlite3_close(database);

    }

    return NO;
}

输出似乎是,函数只是在数组的 39 个元素上添加了几行(我认为是 4)。有人可以帮我这里有什么错误吗?

问候...

最佳答案

您正在准备很多语句,但只执行最后一个。

将电话移至 sqlite3_step进入循环。

请注意,您必须调用 sqlite3_finalize对于您准备的每个语句,无论该步骤是否成功。

不调用sqlite3_close不能跳出函数.

您不得调用 sqlite3_reset声明完成后。

关于objective-c - 使用数组添加批处理值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18468827/

相关文章:

iphone - 从 Objective-C 中的 SQLite 语句检索计数

mysql - Sqlite3 创建表时出错

android - Android 版 SQLite 中预加载数据

objective-c - 您如何使用 Apple 的授权服务取消授权?

c++ - 计算 C 数组中三元组的频率以进行索引

ios - 如何: sort NSArray by date in descending order with nil date left on top of result set

ios - 不兼容的整数到指针的转换

sqlite - 运行 ServiceStack 示例时出现 System.BadImageFormatException。

sqlite - 使用 Xamarin.Forms 从 Sqlite 数据库获取数据

iphone - NSURL连接超时