使用插入或更新查询时,Sqlite数据库被锁定错误5?

时间:2014-07-23 11:44:09

标签: ios multithreading sqlite thread-safety

我创建了具有所有操作CRUD的数据库类。我已成功读取所有数据库,只要一次插入或更新查询fire完成后,插入或更新查询将无法工作,但它已成功读取所有数据。

我已经使用过单身,但它不起作用。

 -(MyClass *)sharedInstance
 {
    static MyClass *sharedInstance=nil;
    static  dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    sharedInstance = [[self alloc] init];
     });
return sharedInstance;
 }     

因为多线程问题,但它不起作用。

我的插入功能

- (NSNumber *) insertRow:(NSDictionary *) record
 {
   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];

NSMutableArray *placeHoldersArray = [NSMutableArray arrayWithCapacity:dictionarySize];

for (int count = 0 ; count < dictionarySize ; count++)
    [placeHoldersArray addObject:@"?"];

NSString *query = [NSString stringWithFormat:@"insert into %@ (%@) values (%@)",tableName,[[record allKeys]componentsJoinedByString:@","],[placeHoldersArray componentsJoinedByString:@","]];

[self bindSQL:[query UTF8String] withVargs:(va_list)dValues.mutableBytes];

sqlite3_step(statment);

if(sqlite3_finalize(statment) == SQLITE_OK)
{

    return [self lastInsertId];
}
else
{
    NSLog(@"doQuery: sqlite3_finalize failed (%s)", sqlite3_errmsg(database));
    return @0;
  }
 }

0 个答案:

没有答案