當我嘗試插入或更新時,數據庫被隨機鎖定。我正在給我使用的代碼。請讓我知道,除此之外還需要做些什麼。數據庫在嘗試更新和插入時被鎖定
我讀了一些發生此問題的原因是由於數據庫的打開和關閉功能。請讓我知道問題是什麼?
-(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'。 任何其他方式做到這一點
就在退貨之前? – user3360389
最後如果條件爲sqlite3_open。因爲如果它是打開的,然後關閉只 – DharaParekh
我真的發現了這個問題。我運行此代碼時,數據庫被鎖定。 – user3360389