2014-03-06 37 views
0

当我尝试插入或更新时,数据库被随机锁定。我正在给我使用的代码。请让我知道,除此之外还需要做些什么。数据库在尝试更新和插入时被锁定

我读了一些发生此问题的原因是由于数据库的打开和关闭功能。请让我知道问题是什么?

-(BOOL)createDB : (const char *)str { 
NSString *docsDir; 
NSArray *dirPaths; 
// Get the documents directory 
dirPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = dirPaths[0]; 
// Build the path to the database file 
databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent: @"SpotLogic.db"]]; 
BOOL isSuccess = YES; 
// NSFileManager *filemgr = [NSFileManager defaultManager]; 
    dbpath = [databasePath UTF8String]; 
    if (sqlite3_open(dbpath, &database) == SQLITE_OK) 
    { 
     char *errMsg; 
     const char *sql_stmt = str; 
     if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg) 
      != SQLITE_OK) 
     { 
      isSuccess = NO; 
      NSLog(@"Failed to create table"); 
     } 
     sqlite3_close(database); 
     return isSuccess; 
    } 
    else { 
     isSuccess = NO; 
     NSLog(@"Failed to open/create database"); 
    } 
return isSuccess; 
} 


- (BOOL)deleteData:(NSString *)deleteSql 
{ 
NSString *docsDir; 
NSArray *dirPaths; 
// Get the documents directory 
dirPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = dirPaths[0]; 
// Build the path to the database file 
databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent: @"SpotLogic.db"]]; 
dbpath = [databasePath UTF8String]; 
if (sqlite3_open(dbpath, &database) == SQLITE_OK) 
{ 
    const char *insert_stmt = [deleteSql UTF8String]; 
    sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL); 
    if (sqlite3_step(statement) == SQLITE_DONE) 
    { 
     return YES; 
     NSLog(@"deleted"); 
    } 
    else { 
     NSLog(@"Not deleted"); 
     return NO; 
    } 
    sqlite3_reset(statement); 
} 
return NO; 
} 


- (BOOL)insertData:(NSString *)insertSQL 
{ 
NSString *docsDir; 
NSArray *dirPaths; 
// Get the documents directory 
dirPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = dirPaths[0]; 
// Build the path to the database file 
databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent: @"SpotLogic.db"]]; 
dbpath = [databasePath UTF8String]; 
if (sqlite3_open(dbpath, &database) == SQLITE_OK) 
{ 
     const char *insert_stmt = [insertSQL UTF8String]; 
     sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL); 
     if (sqlite3_step(statement) == SQLITE_DONE) 
     { 
      return YES; 
      NSLog(@"inserted"); 
     } 
     else { 
      NSLog(@"Not inserted"); 
      return NO; 
     } 
     sqlite3_reset(statement); 
    } 
return NO; 
} 


- (sqlite3_stmt *)fetchData:(NSString *)fetchSQL 
{ 
NSLog(@"%@",fetchSQL); 
NSString *docsDir; 
NSArray *dirPaths; 
// Get the documents directory 
dirPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = dirPaths[0]; 
// Build the path to the database file 
databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent: @"SpotLogic.db"]]; 
// array = [[NSMutableArray alloc]init]; 
dbpath = [databasePath UTF8String]; 
if (sqlite3_open(dbpath, &database) == SQLITE_OK) 
{ 
    const char *fetch_stmt = [fetchSQL UTF8String]; 
    if(sqlite3_prepare_v2(database, fetch_stmt,-1, &statement, NULL)==SQLITE_OK) 
    { 
     sqlite3_reset(statement); 
    } 
    else 
    { 

    } 
} 
return statement; 
    } 


- (BOOL)update:(NSString *)insertSQL 
{ 
NSString *docsDir; 
NSArray *dirPaths; 
// Get the documents directory 
dirPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = dirPaths[0]; 
    // Build the path to the database file 
databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent: @"SpotLogic.db"]]; 
dbpath = [databasePath UTF8String]; 
if (sqlite3_open(dbpath, &database) == SQLITE_OK) 
{ 
    const char *insert_stmt = [insertSQL UTF8String]; 
    sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL); 
    if (sqlite3_step(statement) == SQLITE_DONE) 
    { 
     return YES; 
     NSLog(@"inserted"); 
    } 
    else { 
     NSLog(@"Not inserted"); 
     return NO; 
    } 
    sqlite3_reset(statement); 
} 
return NO; 
} 

我确实发现了这个问题。我运行此代码时,数据库被锁定。

for(int i = 0 ; i < meeting_ids.count ; i ++){ 
    statement = [[DataManger getSharedInstance] fetchData:[NSString stringWithFormat:@"SELECT red_flag FROM meeting_question WHERE meeting_id = '%@'",[meeting_ids objectAtIndex:i]]]; 

    while (sqlite3_step(statement) == SQLITE_ROW) 
    { 
     char *field1=(char *) sqlite3_column_text(statement, 0); 
     NSString *name =[[ NSString alloc]initWithUTF8String:field1]; 
     NSLog(@"%@",name); 
     if([name isEqualToString:@"1"]){ 
      [red_flag replaceObjectAtIndex:i withObject:@"1"]; 
      break; 
     } 
    } 
    } 

This while while with for loop is the reason。 我必须从'meeting_ids'数组中的所有会议中使用会议ID获取'red_flag'。 任何其他方式做到这一点

回答

0

试试这个:如果您每次打开数据库

,然后将其关闭也每次。

所以,写...

sqlite3_close(database); 

这一行中的所有功能。

+0

就在退货之前? – user3360389

+0

最后如果条件为sqlite3_open。因为如果它是打开的,然后关闭只 – DharaParekh

+0

我真的发现了这个问题。我运行此代码时,数据库被锁定。 – user3360389

0

运行语句之后,请使用这两个语句进行重置并敲定语句,然后关闭数据库。

sqlite3_reset(statement); 
sqlite3_finalize(statement); 
sqlite3_close(database); 
相关问题