[英]IPhone Development - sqlite3_bind_int not working
i'm trying to insert some data on a database using this code: 我正在尝试使用此代码在数据库上插入一些数据:
-(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));
}
}
the problem is on line: 问题是在线:
sqlite3_bind_int(stmt, 0, location);
without this line and changing the sql, the code works fine, but when i put this line back, i get this error: 没有这一行并更改sql,代码工作正常,但当我把这行,我得到这个错误:
2010-09-17 10:24:01.032 StockControl[944:207] Error on step: 20
From sqlite3.h: 从sqlite3.h:
#define SQLITE_MISMATCH 20 /* Data type mismatch */
Somebody knows where is my mistake? 有人知道我的错误在哪里?
Regards, Claudio 此致,克劳迪奥
According to the docs for the binding methods in SQLite , the bindings count from one, not zero: 根据SQLite中绑定方法的文档,绑定从一个计数,而不是零:
The second argument is the index of the SQL parameter to be set.
第二个参数是要设置的SQL参数的索引。 The leftmost SQL parameter has an index of 1.
最左边的SQL参数的索引为1。
That might well lead to a type mismatch. 这可能会导致类型不匹配。
Why not use following methods? 为什么不使用以下方法?
NSString *sqlCmd = [NSString stringWithFormat:
@"insert into tbl_location values (%d,'%@','%@','%@')",
location,
aLocation.Description,
aLocation.isActive,
aLocation.sequenceOrder];
const char *sql = [sqlCmd UTF8String];
// ...
I use bind only for large data, for example an image file. 我只对大数据使用bind,例如图像文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.