2010-09-17 91 views
2

我想要使用此代碼插入數據庫上的一些數據:IPhone發展 - sqlite3_bind_int不工作

-(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 */ 

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

問候, 克勞迪奧

回答

9

根據該文檔爲binding methods in SQLite,綁定從一個,不計零:​​

The second argument is the index of the SQL parameter to be set. The leftmost SQL parameter has an index of 1.

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

+0

完美,謝謝:) – Claudio 2010-09-17 00:38:54

1

爲什麼不使用以下方法?

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

const char *sql = [sqlCmd UTF8String]; 

// ... 

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