iPhone开发-sqlite3_bind_int无法正常工作 [英] IPhone Development - sqlite3_bind_int not working

查看:78
本文介绍了iPhone开发-sqlite3_bind_int无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

问题出在网上:

sqlite3_bind_int(stmt, 0, location);

没有此行并更改sql,代码可以正常工作,但是当我放回该行时,出现此错误:

without this line and changing the sql, the code works fine, but when i put this line back, i get this error:

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

从sqlite3.h:

From sqlite3.h:

#define SQLITE_MISMATCH    20   /* Data type mismatch */

有人知道我的错误在哪里?

Somebody knows where is my mistake?

关于, 克劳迪奥

推荐答案

根据 SQLite中的绑定方法,绑定从1开始计数,而不是从0开始计数:

According to the docs for the binding methods in SQLite, the bindings count from one, not zero:

第二个参数是 要设置的SQL参数.这 最左边的SQL参数的索引为 1.

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

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

That might well lead to a type mismatch.

这篇关于iPhone开发-sqlite3_bind_int无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆