使用数组添加批处理值 [英] Adding Batch values with an array

查看:35
本文介绍了使用数组添加批处理值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've trying to insert batch data from array in to Sqlite3 for my mobile project.

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

my code seems to be works fine, but it doesn't insert all data.

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 个).有人可以帮助我这里有什么错误吗?

The output seems to be , function just added few rows on 39 elements of array ( i think it's 4 ) . Can someone help me what's my mistake over here ?

问候...

推荐答案

你准备了很多语句,但只执行最后一个.

Your are preparing lots of statements, but execute only the last one.

将调用 sqlite3_step 移动到循环中.

Move the call to sqlite3_step into the loop.

请注意,无论步骤是否成功,您都必须为您准备的每个语句调用 sqlite3_finalize.

Please note that you must call sqlite3_finalize for every statement you have prepared, regardless of whether the step suceeded or not.

不调用sqlite3_close就不能跳出函数.

You must not jump out of the function without calling sqlite3_close.

您不得在语句完成后调用 sqlite3_reset.

You must not call sqlite3_reset after the statement has been finalized.

这篇关于使用数组添加批处理值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆