首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IPhone开发- sqlite3_bind_int不工作

IPhone开发- sqlite3_bind_int不工作
EN

Stack Overflow用户
提问于 2010-09-17 08:27:13
回答 2查看 7K关注 0票数 2

我正在尝试使用以下代码在数据库中插入一些数据:

代码语言:javascript
复制
-(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));
    }
}

问题出现在网上:

代码语言:javascript
复制
sqlite3_bind_int(stmt, 0, location);

如果没有这一行并更改sql,代码就可以正常工作,但是当我把这一行放回原处时,我得到了这个错误:

代码语言:javascript
复制
2010-09-17 10:24:01.032 StockControl[944:207] Error on step: 20

来自sqlite3.h:

代码语言:javascript
复制
#define SQLITE_MISMATCH    20   /* Data type mismatch */

有人知道我哪里错了吗?

致敬,克劳迪奥

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-17 08:36:37

根据binding methods in SQLite的文档,绑定从1开始计数,而不是从0开始:

第二个参数是要设置的

参数的索引。最左边的SQL参数的索引为1。

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

票数 10
EN

Stack Overflow用户

发布于 2010-09-17 08:43:19

为什么不使用下面的方法呢?

代码语言:javascript
复制
NSString *sqlCmd = [NSString stringWithFormat:
                   @"insert into tbl_location values (%d,'%@','%@','%@')",
                  location, 
                  aLocation.Description, 
                  aLocation.isActive, 
                  aLocation.sequenceOrder];

const char *sql = [sqlCmd UTF8String];

// ...

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3731894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档