有时当我运行我的应用程序时,我收到错误:没有这样的表“tablename”。然后我的数据库变空,应用程序崩溃。
我100%确定该表存在。奇怪的是它不会经常发生,大约1/30因此很难调试。我不使用任何代码来删除表。
这是我打开数据库的方式:
- (id)initWithDB:(NSString*)dbName{
if(self = [super init])
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:dbName];
if (sqlite3_open([writableDBPath UTF8String], &_database) != SQLITE_OK) {
NSLog(@"could not prepare statement: %s\n", sqlite3_errmsg(_database));
NSLog(@"Failed to open database!");
}
else {
//WAL mode on to write and read database at the same time
sqlite3_exec(_database, "PRAGMA journal_mode=WAL;", 0, 0, 0);
}
}
return self;
}