微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQLite操作数据库全过程代码范本

IOS,sqlite批量插入错误

数据库中插入数据的时候,报错:Prepare-error library routine called out of sequence

代码如下,麻烦帮我看看错误出在哪儿了。谢谢

Nsstring *databaseName = @"DB.sqlite";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
    Nsstring *documentsDir = [documentPaths objectAtIndex:0];
    Nsstring *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    sqlite3 *concertsDB;
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath,&concertsDB) == sqlITE_OK)
    {
        sqlite3_exec(concertsDB,"BEGIN TRANSACTION",0);
        const char *sqlStatement = "INSERT INTO concertsData VALUES (?,?,?)";
        sqlite3_stmt *compiledStatement;

        if (sqlite3_prepare_v2(concertsDB,sqlStatement,-1,&compiledStatement,NULL) == sqlITE_OK) {
            int hasError;

            for (int i=0; i<[events count]; i++) {

                sqlite3_bind_text(compiledStatement,1,[[[events objectAtIndex:i] title] UTF8String],sqlITE_TRANSIENT);
                sqlite3_bind_int(compiledStatement,2,[[[events objectAtIndex:i] date] timeIntervalSince1970]);

                sqlite3_bind_text(compiledStatement,3,[[[events objectAtIndex:i] time] UTF8String],sqlITE_TRANSIENT);

                sqlite3_bind_text(compiledStatement,4,[[[events objectAtIndex:i] shortDesription] UTF8String],5,[[[events objectAtIndex:i] conductor] UTF8String],6,[[[events objectAtIndex:i] location] UTF8String],7,[[[events objectAtIndex:i] durations] UTF8String],8,[[[events objectAtIndex:i] works] UTF8String],9,[[[events objectAtIndex:i] solists] UTF8String],10,[[[events objectAtIndex:i] fulltext] UTF8String],sqlITE_TRANSIENT);                    

                sqlite3_bind_text(compiledStatement,11,[[[[events objectAtIndex:i] concertUrl] absoluteString] UTF8String],12,[[[[events objectAtIndex:i] buyUrl] absoluteString] UTF8String],13,[[[events objectAtIndex:i] imageName] UTF8String],sqlITE_TRANSIENT);

                if (sqlite3_step(compiledStatement) != sqlITE_DONE) {
                    hasError=1;
                    NSLog(@"Prepare-error %s",sqlite3_errmsg(concertsDB));
                }

                sqlite3_clear_bindings(compiledStatement);
            }
            sqlite3_reset(compiledStatement);
            if( hasError == 0 ) {
                sqlite3_exec(concertsDB,"COMMIT",0);
            }
            else {
                sqlite3_exec(concertsDB,"ROLLBACK",0);
            }

        }

        sqlite3_close(concertsDB);
    }

1个回答


prettoyic 2012.11.29 13:09

调用**sqlite3_reset**代替调用**sqlite3_clear_bindings**, 然后用当前的调用**sqlite3_finalize** 代替**sqlite3_reset**。

如果再使用statement要先调用**sqlite3_steP**才能调用**sqlite3_reset**。

等全部完成后,在statement中调用**sqlite3_finalize**

已经在每个循环中设置了绑定变量,就不需要再调用**sqlite3_clear_bindings**。

原文地址:https://www.jb51.cc/sqlite/199143.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐