Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we download sqlite database from url and add it into our application as sqlite database?

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.

like image 882
Nitin Avatar asked Mar 12 '12 09:03

Nitin


1 Answers

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.

like image 176
Devang Avatar answered Sep 27 '22 19:09

Devang