Reputation:
I have created database and table through sqlite3 using terminal on macbook, then deploy it with my iPhone application. Its working fine in simulator inserting & retrieving the values from table.
But when i install it on real iPhone it is not working, why? I think it can not get the path of database, then how can i do that?
Upvotes: 0
Views: 238
Reputation: 4171
Are you leaving the database in the base app path?
Because if you are, the actual hardware won't allow you to write to files in that directory, just read. To write to the database, you will first have to copy it to an accessible directory.
I'm doing something similar to this (where filename is an NSString containing the name of the database file):
NSArray *docPaths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [docPaths objectAtIndex:0];
NSString *fullName = [docPath stringByAppendingPathComponent:fileName];
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:fullName])
{
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *defaultName = [path stringByAppendingPathComponent:fileName];
[fm copyItemAtPath:defaultName toPath:fullName error:NULL];
}
Basically, check if the file already exists and copy it from the base bundlePath if it doesn't.
Upvotes: 1