0
-(int)countTheNumberOfDublicatesForType:(int)typeID
{
NSInteger quantity=0;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:user_data];
sqlite3* database = NULL;
sqlite3_stmt *statement;

if(sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
    const char *sqlQuery=sqlite3_mprintf("SELECT COUNT(refID)\
                                         FROM dublicates\
                                         WHERE typeREF=%i",typeID);

    if(sqlite3_prepare_v2(database, sqlQuery,-1, &statement, NULL) == SQLITE_OK)
        sqlite3_bind_int(statement, 1,typeID);

        if(sqlite3_step(statement) == SQLITE_ROW)
        {
            quantity = sqlite3_column_int(statement, 0);
        }

        sqlite3_finalize(statement);
        sqlite3_free((char*)sqlQuery);
        sqlite3_close(database);
}
else
{
    //
}

return quantity;
}

此方法中的数学运算返回 int 的最大值。错误在哪里以及如何以从数据库返回实际值的方式管理它?先感谢您。

编辑:在此处输入图像描述

4

1 回答 1

0

您没有错误处理,您绑定到一个不存在的参数,代码缩进错误,并且您正在读取错误的数据类型。

使用这样的东西:

if (sqlite3_open([path UTF8String], &database) != SQLITE_OK) {
    sqlite3_errmsg(database); // log or print this
    sqlite3_close(database);
    return 0;
}
if (sqlite3_prepare_v2(database,
                       "SELECT COUNT(refID) FROM dublicates WHERE typeREF=?",
                       -1, &statement, NULL) != SQLITE_OK) {
    sqlite3_errmsg(database); // log or print this
    sqlite3_close(database);
    return 0;
}
sqlite3_bind_int(statement, 1, typeID);
if (sqlite3_step(statement) != SQLITE_ROW)
    sqlite3_errmsg(database); // log or print this
else
    quantity = sqlite3_column_int(statement, 0);
sqlite3_finalize(statement);
sqlite3_close(database);
于 2013-06-01T09:07:26.500 回答