我的更新功能有什么问题?在sqlite数据库锁定错误后更新完美?

时间:2014-07-24 06:52:14

标签: ios sqlite thread-safety

- (void) updateTimeStamp:(NSDictionary *) record forRowID: (NSString *) updateTableName
  {
    int dictionarySize = [record count];

NSMutableData *dKeys = [NSMutableData dataWithLength:sizeof(id) *dictionarySize];

NSMutableData * dValues = [NSMutableData dataWithLength: sizeof(id) * dictionarySize];
[record getObjects:(__unsafe_unretained id *)dValues.mutableBytes andKeys:(__unsafe_unretained id *)dKeys.mutableBytes];

[dValues appendBytes:&updateTableName length:sizeof(id)];

NSString * query = [NSString stringWithFormat:@"update %@ set %@ = ? where  Table_Name= ?",tableName,[[record allKeys] componentsJoinedByString:@" = ?, "]];
[self bindSQL:[query UTF8String] withVargs:(va_list)dValues.mutableBytes];


if(sqlite3_step(statment) == SQLITE_DONE);
{
     NSLog(@"prepead Timestamp %s",sqlite3_errmsg(database));
}
sqlite3_reset(statment);
@synchronized(self)
{

    if(sqlite3_finalize(statment) != SQLITE_OK)
    {
        NSLog(@"doQuery: sqlite3_finalize failed (%s)", sqlite3_errmsg(database));

    }

}

 }

当我运行功能完美的应用程序时?没有任何问题,但数据库被锁定后的功能?该功能有什么问题请给我解决方案

我有很多表,所以我在关闭数据库后更新所有时间戳时,在函数中打开数据库一次。

下面的bindSQL函数

  - (void) bindSQL:(const char *) cQuery withVargs:(va_list)vargs
  {
    int param_count;

NSLog(@"%d",sqlite3_prepare_v2(database, cQuery, -1, &statment, NULL));
if(sqlite3_prepare_v2(database, cQuery, -1, &statment, NULL) != SQLITE_OK)
{
    NSLog(@"bindSQL: could not prepare statement (%s) %s", sqlite3_errmsg(database), cQuery);
    statment = NULL;
    return;
}

if((param_count = sqlite3_bind_parameter_count(statment)))
{
    for(int i =0; i < param_count ;i++)
    {
        id object = va_arg(vargs,id);

        if(object == nil)
        {
            sqlite3_bind_null(statment, i+1);
        }
        else if ([object respondsToSelector:@selector(objCType)])
        {
            if(strchr("islqISLBQ", *[object objCType]))
            {
                sqlite3_bind_int(statment, i+1, [object intValue]);
            }
            else if (strchr("fd", *[object objCType]))
            {
                sqlite3_bind_double(statment, i+1, [object doubleValue]);
            }
            else
            {
                NSLog(@"bindSQL: Unhandled objCType: %s query: %s", [object objCType], cQuery);
                statment = NULL;
                return;
            }
        }
        else if ([object respondsToSelector:@selector(UTF8String)])
        {
            sqlite3_bind_text(statment, i+1, [object UTF8String], -1, SQLITE_TRANSIENT);
        }
        else if ([object isEqual:[NSNull null]])
        {
            statment = NULL;
            return;
        }
        else
        {
            NSLog(@"bindSQL: Unhandled parameter type: %@ query: %s", [object class], cQuery);
            statment = NULL;
            return;
        }
    }
}

va_end(vargs);
return;
 }

1 个答案:

答案 0 :(得分:0)

此代码创建两个语句对象:

NSLog(@"%d",sqlite3_prepare_v2(database, cQuery, -1, &statment, NULL));
if(sqlite3_prepare_v2(database, cQuery, -1, &statment, NULL) != SQLITE_OK)

其中只有一个稍后定稿。

如果要检查返回码,最好使用变量:

int rc = sqlite3_prepare_v2(database, cQuery, -1, &statment, NULL);
NSLog(@"%d", rc);
if (rc != SQLITE_OK)