开发者

sqlite database resets on each compile and run

开发者 https://www.devze.com 2023-03-03 13:18 出处:网络
I have built up a basic database application in which user can insert records onto a table and app displays them on a TableView.

I have built up a basic database application in which user can insert records onto a table and app displays them on a TableView.

Everything is working as it is supposed to be. For example, the new records do display even if we kill the app from app switcher and relaunch it from the springboard.

BUT every time I build and run using Xcode, the database just goes to default records! The new records are just not there.

Is it normal?... but what if I want to test my app for new records? Any fix?

BTW, JFYI, below is the code I use to make editable DB.

-(NSString *)createEditableDatabase{
// Check if DB already exists
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;

NSArray *paths = NSSearchPathF开发者_StackOverflow社区orDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *writableDB = [documentsDir stringByAppendingPathComponent:@"Database.db"];

success = [fileManager fileExistsAtPath:writableDB];

//The editable DB already exists
if (success) {
    return writableDB;
}


//The editable DB does not exist
//Copy the default DB into App's Doc Dir.

NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Database.db"];

success = [fileManager copyItemAtPath:defaultPath toPath:writableDB error:&error];

if (!success) {
    NSAssert1(0, @"Failed to create writable DB file: '%@'", [error localizedDescription]);
}
return writableDB;

}


While digging deeper, I noticed that the database modify date in finder was not updating when I inserted a record. So, I found out that I was still using old path to perform DB operations (not the Documents one). :) Now everything working fine. Anyways thanks Nick

0

精彩评论

暂无评论...
验证码 换一张
取 消