FMDB:错误 14,无法打开数据库文件
Posted
技术标签:
【中文标题】FMDB:错误 14,无法打开数据库文件【英文标题】:FMDB: error 14, unable to open database file 【发布时间】:2013-01-03 01:46:48 【问题描述】:目前我正在为我的 iPhone 应用程序使用 FMDB 包装器。我的问题是在执行 2000 条 SQL 插入语句后(5000 到 8000 条 sql 语句中),出现以下错误:
“错误14,无法打开数据库文件”
谁能帮我解决这个错误?
这是我的代码:
-(BOOL)insertOrUpdateinTable:(NSString *)tableName:(NSMutableArray *)columnName: (NSMutableArray *)columnValue
[self initiateFMDB];
NSString *queryString=@"";
BOOL success;
//queryString =[[NSString alloc]init];
if (![db open])
NSLog(@"could not open db");
else
//insert into Category(Categorycode,CategoryDesc) values (1,2)
queryString=[queryString stringByAppendingFormat:@"insert into %@(",tableName];
for (int cntCName=0; cntCName<[columnName count]; cntCName++)
if (cntCName<[columnName count]-1)
queryString=[queryString stringByAppendingFormat:@"%@,",[columnName objectAtIndex:cntCName]];
else
queryString=[queryString stringByAppendingFormat:@"%@",[columnName objectAtIndex:cntCName]];
queryString=[queryString stringByAppendingString:@") values ("];
for (int cntCValue=0; cntCValue<[columnValue count]; cntCValue++)
if (cntCValue<[columnValue count]-1)
queryString=[queryString stringByAppendingFormat:@"'%@',",[columnValue objectAtIndex:cntCValue]];
else
queryString=[queryString stringByAppendingFormat:@"'%@'",[columnValue objectAtIndex:cntCValue]];
queryString=[queryString stringByAppendingString:@")"];
// NSLog(@"QueryString=%@",queryString);
@try
success=[db executeUpdate:[NSString stringWithFormat:@"%@",queryString]];
@catch (NSException *exception)
NSLog(@"Exception error for selectFromItemPhoto is %@",[exception reason]);
if (success==TRUE)
NSLog(@"Data inserted successfully");
else
NSLog(@"Data is not inserted successfully");
// queryString=nil;
// [queryString release];
if ([db open])
[db close];
return success;
【问题讨论】:
不确定这是否是您的问题,但您应该考虑保持连接打开,而不是在每次插入时打开和关闭。 @bryanmacn 可能是问题.. 可能在内部 FMDB 耗尽了 io 句柄 【参考方案1】:我刚刚重新格式化了您的代码并删除了某些内容。尝试打印查询字符串,看看有没有问题。
尝试以下方法:
-(BOOL)insertOrUpdateinTable:(NSString *)tableName withColumns:(NSMutableArray *)columnName withValues:(NSMutableArray *)columnValue
[self initiateFMDB];
NSString *queryString=@"";
BOOL success;
if (![db open])
NSLog(@"could not open db");
return NO; //no need to proceed if DB could be opened.
//insert into Category(Categorycode,CategoryDesc) values (1,2)
queryString=[queryString stringByAppendingFormat:@"insert into %@(",tableName];
for (int cntCName=0; cntCName<[columnName count]; cntCName++)
if (cntCName<[columnName count]-1)
queryString=[queryString stringByAppendingFormat:@"%@,",[columnName objectAtIndex:cntCName]];
else
queryString=[queryString stringByAppendingFormat:@"%@",[columnName objectAtIndex:cntCName]];
//first for loop ends here
queryString=[queryString stringByAppendingString:@") values ("];
for (int cntCValue=0; cntCValue<[columnValue count]; cntCValue++)
if (cntCValue<[columnValue count]-1)
queryString=[queryString stringByAppendingFormat:@"'%@',",[columnValue objectAtIndex:cntCValue]];
else
queryString=[queryString stringByAppendingFormat:@"'%@'",[columnValue objectAtIndex:cntCValue]];
//second for loop ends here
queryString=[queryString stringByAppendingString:@")"];
NSLog(@"QueryString=%@",queryString);
@try
success=[db executeUpdate:[NSString stringWithFormat:@"%@",queryString]];
@catch (NSException *exception)
NSLog(@"Exception error for selectFromItemPhoto is %@",[exception reason]);
if (success==YES)
NSLog(@"Data inserted successfully");
else
NSLog(@"Data is not inserted successfully");
[db close];
return success;
【讨论】:
以上是关于FMDB:错误 14,无法打开数据库文件的主要内容,如果未能解决你的问题,请参考以下文章
Laravel - php artisan migrate - SQLSTATE[HY000] [14] 无法打开数据库文件