Reputation: 2741
I have to run my App on off line mode too, so thats why trying to copy myDatabase.db file into app's document directory.
i have create myDatabase.db from terminal and also populate it. It has almost 10 tables, Now i want to add it to my app,
i have tried it but just Login record Table is copied from whole database file not any other table. Like i login to my app , it takes data from Login table and no more table data is shown. even NSLog for sqlite Error is said that Error no such table: TableName ,that means no other table is copied.
**Question is that how to copy the complete myDatabase.db File?
this is what i have did to copy the file…
in my .m file i add this method
- (void) copyDatabaseIfNeeded
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
dBPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dBPath];
if(!success)
{
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myDatabase.db"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dBPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
- (NSString *) getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSLog(@"dbpath : %@",documentsDir);
return [documentsDir stringByAppendingPathComponent:@"myDatabase.db"];
}
in my ViewDidLoad method
[self copyDatabaseIfNeeded];
database = [dBPath UTF8String];
then i login and move to next ViewController taking the dBpath with it to new View. i have checked on all ViewControllers the dBPath is okay, but not the records shown as it should be,
One more thing if i change the dBPath to my Computer's document Directory, where this database is located then it works perfectly…
now please help me to resolve this issue. any help would be greatly appreciated..
Upvotes: 0
Views: 269
Reputation: 649
Try using SQLite Database Browser to check if the table has been created and the data has been appended in the table after the database has been copied to the application's document directory.
To access the application's document directory, run the application on the simulator and go to Library->Application Support->iPhone Simulator-> (Simulator Version) -> Applications-> Search For ur application -> Documents Directory->mydatabase.db
Upvotes: 1