代码之家  ›  专栏  ›  技术社区  ›  Claudio

IPhone开发-sqlite3\u bind\u int不工作

  •  2
  • Claudio  · 技术社区  · 16 年前

    -(void)insertLocationOnDatabase:(LocationType *)aLocation {
        sqlite3_stmt *stmt;
        int location = [aLocation.locationID intValue];
        NSLog(@"Location ID: %i", location);
        const char *sql = "insert into tbl_location values (?,?,?,?)";
        if (sqlite3_prepare_v2(database, sql, -1, &stmt, NULL) == SQLITE_OK) {
            sqlite3_bind_int(stmt, 0, location);
            sqlite3_bind_text(stmt, 1, [aLocation.Description UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 2, [aLocation.isActive UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 3, [aLocation.sequenceOrder UTF8String], -1, SQLITE_TRANSIENT);
            if (sqlite3_step(stmt) == SQLITE_DONE) {
                NSLog(@"Location %@ inserted on database",aLocation.Description);
            }
            else {
                NSLog(@"Error on step: %i",sqlite3_errcode(database));
                }
        }
        else {
            NSLog(@"Error on prepare: %i",sqlite3_errcode(database));
        }
    }
    

    问题在于:

    sqlite3_bind_int(stmt, 0, location);
    

    2010-09-17 10:24:01.032 StockControl[944:207] Error on step: 20
    

    从sqlite3.h:

    #define SQLITE_MISMATCH    20   /* Data type mismatch */
    

    有人知道我的错在哪里?

    当做,

    2 回复  |  直到 16 年前
        1
  •  10
  •   martin clayton egrunin    16 年前

    binding methods in SQLite ,绑定从1开始计数,而不是从零开始计数:

    第二个参数是 最左边的SQL参数的索引为 1

    这很可能导致类型不匹配。

        2
  •  1
  •   AechoLiu    16 年前

    为什么不使用以下方法?

    NSString *sqlCmd = [NSString stringWithFormat:
                       @"insert into tbl_location values (%d,'%@','%@','%@')",
                      location, 
                      aLocation.Description, 
                      aLocation.isActive, 
                      aLocation.sequenceOrder];
    
    const char *sql = [sqlCmd UTF8String];
    
    // ...
    

    我只对大数据使用bind,例如图像文件。