程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> cocoa touch-IOS,SQLite批量插入錯誤

cocoa touch-IOS,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, 0, 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], -1, SQLITE_TRANSIENT);
                sqlite3_bind_int(compiledStatement, 2, [[[events objectAtIndex:i] date] timeIntervalSince1970]);

                sqlite3_bind_text(compiledStatement, 3, [[[events objectAtIndex:i] time] UTF8String], -1, SQLITE_TRANSIENT);

                sqlite3_bind_text(compiledStatement, 4, [[[events objectAtIndex:i] shortDesription] UTF8String], -1, SQLITE_TRANSIENT);

                sqlite3_bind_text(compiledStatement, 5, [[[events objectAtIndex:i] conductor] UTF8String], -1, SQLITE_TRANSIENT);

                sqlite3_bind_text(compiledStatement, 6, [[[events objectAtIndex:i] location] UTF8String], -1, SQLITE_TRANSIENT);

                sqlite3_bind_text(compiledStatement, 7, [[[events objectAtIndex:i] durations] UTF8String], -1, SQLITE_TRANSIENT);

                sqlite3_bind_text(compiledStatement, 8, [[[events objectAtIndex:i] works] UTF8String], -1, SQLITE_TRANSIENT);

                sqlite3_bind_text(compiledStatement, 9, [[[events objectAtIndex:i] solists] UTF8String], -1, SQLITE_TRANSIENT);

                sqlite3_bind_text(compiledStatement, 10, [[[events objectAtIndex:i] fulltext] UTF8String], -1, SQLITE_TRANSIENT);                    

                sqlite3_bind_text(compiledStatement, 11, [[[[events objectAtIndex:i] concertUrl] absoluteString] UTF8String], -1, SQLITE_TRANSIENT);

                sqlite3_bind_text(compiledStatement, 12, [[[[events objectAtIndex:i] buyUrl] absoluteString] UTF8String], -1, SQLITE_TRANSIENT);

                sqlite3_bind_text(compiledStatement, 13, [[[events objectAtIndex:i] imageName] UTF8String], -1, 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, 0, 0);
            }
            else {
                sqlite3_exec(concertsDB, "ROLLBACK", 0, 0, 0);
            }

        }

        sqlite3_close(concertsDB);
    }

最佳回答:


用調用**sqlite3_reset**代替調用**sqlite3_clear_bindings**, 然後用當前的調用**sqlite3_finalize** 代替**sqlite3_reset**。

如果再使用statement要先調用**sqlite3_step**才能調用**sqlite3_reset**。

等全部完成後,在statement中調用**sqlite3_finalize**

已經在每個循環中設置了綁定變量,就不需要再調用**sqlite3_clear_bindings**。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved