iphone - 无法重置资源语句或进入无限循环

标签 iphone objective-c ios sqlite

当我试图关闭 SQLite 数据库时,我发现我遇到了代码错误 5。
所以我努力重置所有使用的资源。
我相信这是我没有正确重置所有资源的方法:

- (NSArray*) allObjects
{
    NSMutableArray* objects=[NSMutableArray new];
    // resource is an ivar of type sqlite3_stmt* , it has been initialized with   
    // sqlite3_prepare, a query where I select some rows of a table
    if(!resource)
    {
        return nil;
    }
    while(sqlite3_step(resource)== SQLITE_ROW)
    {
        NSMutableDictionary* object=[NSMutableDictionary new];
        int count= sqlite3_column_count(resource);
        for(int i=0; i<count; i++)
        {
            // I need to know the type and the name of all columns to build
            // a dictionary object.Later I'll optimize it doing this only at 
            // the first loop iteration.
            const char* key=sqlite3_column_name(resource, i);
            int type= sqlite3_column_type(resource, i);
            const unsigned char* text;
            double value;
            switch (type)
            {
                case SQLITE_TEXT:
                    text=sqlite3_column_text(resource, i);
                    [object setObject: [NSString stringWithFormat: @"%s",text] forKey: [NSString stringWithFormat: @"%s",key]];
                    break;
                case SQLITE_INTEGER:
                    value= sqlite3_column_int(resource, i);
                    [object setObject: @(value) forKey: [NSString stringWithFormat: @"%s",key]];
                    break;
                case SQLITE_FLOAT:
                    value= sqlite3_column_double(resource, i);
                    [object setObject: @(value) forKey: [NSString stringWithFormat: @"%s",key]];
                    break;
                case SQLITE_NULL:
                    [object setObject: [NSNull null] forKey: [NSString stringWithFormat: @"%s",key]];
                    break;
                default:
                    break;
            }
        }
        // sqlite3_reset(resource);  Point 1
        [objects addObject: object];
    }
    // sqlite3_reset(resource);  Point 2
    return objects;
}

所以如果我在关闭数据库时将 sqlite3_reset(resource) 放在第 2 点,我会得到错误 5,如果我把它放在第 1 点,我会进入无限循环。
我几乎可以肯定问题出在这个函数中,我也在用数据库做其他事情,所以错误可能出在代码的其他部分,但那是很多代码。所以如果问题不在这里写它在评论中,我将发布代码的其他“可疑”部分。

最佳答案

你应该把“重置”放在#2 点。然后当你不再需要它时,你应该在准备好的语句上调用 sqlite3_finalize。执行此操作后,您还应该将 ivar 设置为 nil

sqlite3_reset 表示您已准备好再次使用该语句。 sqlite3_finalize 表示您已完全完成该语句,应该清理其资源。

错误 5 是 SQLITE_BUSY。如果“完成”没有解决问题,请搜索 SQLITE_BUSY 的原因。

关于iphone - 无法重置资源语句或进入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13897666/

相关文章:

iphone - 如何取消 Twitter 与我的 iOS 应用程序的链接

iphone - 如何在 iPad 中获取实际的 pdf 页面大小?

ios - 当非 void 方法在 Objective-C 中结束时会发生什么?

ios - dispatch_after 会阻塞 UI 吗?

ios - Hashchange 导致 phonegap 应用程序崩溃?

iphone - UITableView 和使用 NSURLconnection 的异步加载

android - "Add To Home Screen"如何打开没有浏览器组件的网页?

ios - 使用 UICollectionView 显示全屏图像

objective-c - 在钥匙串(keychain)/用户默认值中保存原子属性有什么好处吗?

ios - iOS 中的竞争条件使用 GCD 用于 UICollectionView