Nitin
Nitin

Reputation: 7461

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.

Upvotes: 9

Views: 7071

Answers (2)

shripad20
shripad20

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

Devang
Devang

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

Related Questions