试图更新和插入时,数据核心会被锁定
Databse getting locked when trying to update and insert
当我尝试插入或更新时,数据库被随机锁定。我正在提供我正在使用的代码。请让我知道除此之外是否需要做其他事情。
我阅读了由于数据库的开放和密切功能而发生的一些问题。请让我知道问题是什么?
-(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);
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何找出GDB的SIGTRAP核心转储的根本原因
- C++映射分割错误(核心转储)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 尽管测试成功,CppUnit测试核心仍被丢弃.为什么
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 从文件中读取的数据,但发生核心转储错误
- 数据包大小(每数据包 mBytes)或核心音频中 AAC 文件的比特率
- 试图更新和插入时,数据核心会被锁定
- 分割故障(核心倾倒)在使用地图存储有序数据时
- 无法使用sqlite3数据库表的所有行中的数据填充列表,错误:跟踪/断点陷阱(核心转储)