簡體   English   中英

iPhone開發 - sqlite3_bind_int不能正常工作

[英]IPhone Development - sqlite3_bind_int not working

我正在嘗試使用此代碼在數據庫上插入一些數據:

-(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);

沒有這一行並更改sql,代碼工作正常,但當我把這行,我得到這個錯誤:

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

從sqlite3.h:

#define SQLITE_MISMATCH    20   /* Data type mismatch */

有人知道我的錯誤在哪里?

此致,克勞迪奧

根據SQLite中綁定方法的文檔,綁定從一個計數,而不是零:

第二個參數是要設置的SQL參數的索引。 最左邊的SQL參數的索引為1。

這可能會導致類型不匹配。

為什么不使用以下方法?

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

const char *sql = [sqlCmd UTF8String];

// ...

我只對大數據使用bind,例如圖像文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM