SQLite插入错误的值。iOS开发

SQLite插入错误的值。iOS开发,ios,objective-c,sqlite,Ios,Objective C,Sqlite,我越来越熟悉iOS上的SQLite。我正在尝试进行simp用户注册,但年龄值(NSInteger)总是变成一些大的负数,如-1073752692。 这是我的注册代码: - (User*) registerUser:(NSString *) firstname lastname:(NSString *) lastname age:(NSInteger) Age email:(NSString

我越来越熟悉iOS上的SQLite。我正在尝试进行simp用户注册,但年龄值(NSInteger)总是变成一些大的负数,如-1073752692。 这是我的注册代码:

    - (User*) registerUser:(NSString *) firstname
              lastname:(NSString *) lastname
                   age:(NSInteger) Age
                 email:(NSString *) email
              password:(NSString *) password
               picture:(NSString *) picture
{
    User* user = nil;
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO User (firstname, lastname, age, email, password, picture) VALUES (\"%@\", \"%@\", \"%d\", \"%@\", \"%@\", \"%@\")", firstname, lastname, Age, email, password, picture];

    sqlite3_stmt *insert;
    int result = sqlite3_prepare_v2(self.db, [sql UTF8String], -1, &insert, NULL);
    if (result == SQLITE_OK) {
        sqlite3_step(insert);
        user = [[User alloc]init];
        user.firstname = firstname;
        user.lastname = lastname;
        user.age = Age;
        user.password = password;
        user.picture = picture;
        NSLog(@"Successful registration!");

    } else {
        NSLog(@"Error: insert prepare statement failed: %s.", sqlite3_errmsg(self.db));
    }
    sqlite3_finalize(insert);
    return user;

} 
我想不出为什么会这样。我过去经常和博士后一起工作,从来没有遇到过这样的问题

谢谢。

说明您不应将
NSInteger
%d
一起使用

在任何情况下,为了防止像这样的格式问题和SQL注入攻击(任何包含
的字符串都会使代码爆炸),您应该使用以下参数:

NSString *sql = @"INSERT INTO User(firstname,lastname,age,email,password,picture)"
                 " VALUES (?,?,?,?,?,?)";
int result = sqlite3_prepare_v2(self.db, [sql UTF8String], -1, &insert, NULL);
if (result == SQLITE_OK) {
    sqlite3_bind_text(insert, 1, [firstname UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(insert, 2, [lastname UTF8String],  -1, SQLITE_TRANSIENT);
    sqlite3_bind_int (insert, 3, Age);
    sqlite3_bind_text(insert, 4, [email UTF8String],     -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(insert, 5, [password UTF8String],  -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(insert, 6, [picture UTF8String],   -1, SQLITE_TRANSIENT);
    result = sqlite3_step(insert);
    ...

(您还需要检查
sqlite3\u step
的返回值)

为什么您的年龄变量是大写的?看起来像是一个类型。您好,谢谢,我使用大写形式(没有明显的原因:)请注意,如果您只需记录
sql
值,您就会看到问题所在。