Reputation: 7461
I used sqlite database in my application.I want to synchronize this database with my mysql server.But i think it is easy to replace exiting database with new database in my application.So,It will also solve data import export problem.But i don't know how download .sqlite file from my url and add it into in my application bundle.
In simple way.I want to add file in Xcode Resource folder at runtime.I don't how to do it. Please help me if anybody have idea.
Thanks In Advance.
Upvotes: 9
Views: 7071
Reputation: 858
To add downloaded database to your application as sqlite database try following code
NSString *dbfilepath = [FileUtils documentsDirectoryPathForResource:[NSString stringWithFormat:@"%@.db", @"downloadedDatabase"]];
NSData *dbData = [NSData dataWithContentsOfFile:dbfilepath];
NSString *filePath = [[FileUtils documentsDirectoryPath] stringByAppendingPathComponent:@"Database.sqlite"];
[dbData writeToFile:filePath atomically:YES];
Now your application database is ready with imported records.
Upvotes: 0
Reputation: 11338
Try following code :
NSData *dbFile = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.someurl.com/DatabaseName.sqlite"]];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Database.sqlite"];
[dbFile writeToFile:filePath atomically:YES];
Now you can use this database file.
Upvotes: 7