【问题标题】:SQLite3 library routine called out of sequenceSQLite3 库例程调用乱序
【发布时间】:2013-04-01 00:51:10
【问题描述】:

以下代码给了我一个库例程调用乱序错误,但我无法解释问题出在哪里。有任何想法吗 ?

- (BOOL)insertProduct:(Product *)product inOrder:(Order *)order withAmount:(int)amount
    {
        BOOL ok = NO;
        sqlite3_stmt *statement;

        const char *dbpath = [_databasePath UTF8String];

        if (sqlite3_open(dbpath, &_database) == SQLITE_OK)
        {
            NSString * insertSQL;
            int amount = [self getAmountForProduct:product inOrder:order];
            NSLog(@"%i", amount);

            if (amount != -1)
            {
                insertSQL = [NSString stringWithFormat: @"UPDATE ARTICOLIPERORDINE SET quantita = %i WHERE ordine = %i AND articolo = '%@'", amount, order.idOrdine, product.codice];
            }
            else
            {
                insertSQL = [NSString stringWithFormat: @"INSERT INTO ARTICOLIPERORDINE (ordine, articolo, quantita) VALUES(%i, '%@', %i)",order.idOrdine, product.codice, 1];
            }

            NSLog(@"%@", insertSQL);
            const char *insert_stmt = [insertSQL UTF8String];

            if (sqlite3_prepare_v2(_database, insert_stmt, -1, &statement, NULL) == SQLITE_OK)
            {
                if (sqlite3_step(statement) == SQLITE_DONE)
                {
                    ok = YES;
                }
                else
                {
                    ok = NO;
                    NSLog(@"sqlite3 ERROR %s: %@",sqlite3_errmsg(_database), insertSQL);
                }

                sqlite3_finalize(statement);
                sqlite3_close(_database);
            }
            else
            {
                NSLog(@"Error prepare = %s", sqlite3_errmsg(_database));
            }

        }

        return ok;
    }

日志打印错误准备 = 库例程调用乱序

【问题讨论】:

  • 你调用sqlite3_prepare_v2的返回值是多少?
  • 一个 BOOL ... 问题已更新
  • 实际上,根据文档,sqlite3_prepare_v2 在成功时返回SQLITE_OK,所以如果你运行上面的代码,它肯定会失败。首先,您在失败时调用 step(SQLITE_OK 为 0),其次,您在准备好的语句上调用 sqlite3_finalize。
  • sqlite3_close(_database);应该在准备调用下方关闭。除此之外,您能否提供您尝试执行的 sql 语句,可能是您没有转义 sql。

标签: ios objective-c sqlite


【解决方案1】:

我有一种方法。 在此之前,请确保您已完成所有 sql 查询,例如连接打开、关闭、完成等。

在将查询实际运行到代码中之前,在任何数据库浏览器中执行该查询,您将看到查询中缺少什么。

就我而言,我错误地将列名放入查询中并运行代码,但我多次收到错误。我仔细检查了所有代码并在数据库浏览器中执行查询,发现我的错误。

【讨论】:

    猜你喜欢
    • 2011-04-02
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 2023-03-14
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多