试图更新和插入时,数据核心会被锁定

Databse getting locked when trying to update and insert

本文关键字:核心 数据 锁定 更新 插入      更新时间:2023-10-16

当我尝试插入或更新时,数据库被随机锁定。我正在提供我正在使用的代码。请让我知道除此之外是否需要做其他事情。

我阅读了由于数据库的开放和密切功能而发生的一些问题。请让我知道问题是什么?

-(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;
        }
    }
   }

这是原因。我必须从所有会议中获得" red_flag",其中包含" Meeting_ids"阵列中的会议ID。任何其他方法

尝试以下:

如果您每次都打开数据库,则每次也将其关闭。

所以,写...

sqlite3_close(database);

所有功能中的这一行。

在运行语句后,请使用这两个语句进行重置和最终确定语句,然后关闭数据库。

sqlite3_reset(statement);
sqlite3_finalize(statement);
sqlite3_close(database);