数据库连接代码出错,Xcode 4& SQLITE3

时间:2012-10-06 16:42:16

标签: objective-c xcode4 sqlite

我一直收到一条错误,上面写着“被调用的类型NSString不是函数或函数指针。我已经被困了几天尝试。我知道我的问题在哪里?

- (void)saveData
{

    sqlite3_stmt    *statement;
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &testdb1) == SQLITE_OK)
    {


        NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO TESTDB1"
                               (email, username, password, age) VALUES (\"%@\", \"%@\", \"%@\", \"%@\")",
                                email.text, username.text, password.text, age.integer];


        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(testdb1, insert_stmt, -1, &statement, NULL);

        if (sqlite3_step(statement) == SQLITE_DONE)
        {
           status.text = @"User added";
           email.text = @"";
           username.text = @"";
           password.text = @"";
           age.text = @"";
        } else {
           status.text = @"Failed to add user";
        }
          sqlite3_finalize(statement);
          sqlite3_close(testdb1);
    }

}

2 个答案:

答案 0 :(得分:2)

NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO TESTDB1"
                   (email, username, password, age)

你过早地关闭了引号;如果仔细观察表达式,可以注意到

@"INSERT INTO TESTDB" (email, username, password, age)

因此,编译器会发现您正在尝试将NSString作为函数调用。将格式字符串文字更改为

NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO TESTDB1 (email, username, password, age) VALUES (\"%@\", \"%@\", \"%@\", \"%@\")",
                    email.text, username.text, password.text, age.integer];

你应该没事。

P上。 s。:在发布问题之前看看预览 - 它的格式非常简洁。

答案 1 :(得分:1)

您在stringWithFormat调用的第二行的开头缺少引号。