I am working on an app that will be pulling data from a SQLite Db locally. The app is running file but as I am testing, I add more records and such but for some reason both the simulator and the Device only see the last copy.
I have done a CLEAN build, removed the .DB from the project and then added it back, but to no avail. If the DB has 5 records, I update/add t开发者_如何学Pythonwo more records, those new records are not showing.
About the only way I can get them to be seen is to remove the .DB from project, rename the DB to another name, then add back.
I know this sounds like some type of cache issue but am unable to figure out how to clear it out. As I mentioned, does same for both simulator and device.
Any help is greatly appreciated. Thanks in advance.
Geo...
If you have a db file in your resources, you need to copy it from resources to either library or document on launch and then connect to the one in documents or library.
Hint: log out the path your connecting to, open that in terminal (it will be under Derived Data) and then connect and query using the sqlite3 cmd line (search). That helps keep you're sanity that you're operating on the right db and you can inspect your data as you go.
As a side note, see this post (I have a library which handles all this if you're interested): Don't wan't to replacing the old database when app is updated
Here's an example from a sample app where I call ensureOpened (which handles the copy from resource and logs what I'm using):
- (BOOL)ensureDatabaseOpen: (NSError **)error
{
// already created db connection
if (_contactDb != nil)
{
return YES;
}
NSLog(@">> ContactManager::ensureDatabaseOpen");
if (![self ensureDatabasePrepared:error])
{
return NO;
}
const char *dbpath = [_dbPath UTF8String];
if (sqlite3_open(dbpath, &_contactDb) != SQLITE_OK &&
error != nil)
{
*error = [[[NSError alloc] initWithDomain:@"ContactsManager" code:1000 userInfo:nil] autorelease];
return NO;
}
NSLog(@"opened");
return YES;
}
- (BOOL)ensureDatabasePrepared: (NSError **)error
{
// already prepared
if ((_dbPath != nil) &&
([[NSFileManager defaultManager] fileExistsAtPath:_dbPath]))
{
return YES;
}
// db in main bundle - cant edit. copy to library if !exist
NSString *dbTemplatePath = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"db"];
NSLog(@"%@", dbTemplatePath);
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
_dbPath = [libraryPath stringByAppendingPathComponent:@"contacts.db"];
NSLog(@"dbPath: %@", _dbPath);
// copy db from template to library
if (![[NSFileManager defaultManager] fileExistsAtPath:_dbPath])
{
NSLog(@"db not exists");
NSError *error = nil;
if (![[NSFileManager defaultManager] copyItemAtPath:dbTemplatePath toPath:_dbPath error:&error])
{
return NO;
}
NSLog(@"copied");
}
return YES;
}
Hope that helps
Sounds like a tricky one to debug. May I ask how you are connecting to the database? Are you using CoreData or raw SQL statements?
I have had a lot of pain understanding how databases work with CoreData so I might be able to help.
Is the database file part of your application bundle? You can't modify your application bundle. Copy the database out to a location that you can modify it, such as your Documents directory, or just create it there at runtime when you first run.
精彩评论