9

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.

Nitin
  • 7,455
  • 2
  • 32
  • 51
  • 1
    Why would you download the .sqlite file? Wouldn't you query the MySQL server and then create the local schema and populate the tables? – trojanfoe Mar 12 '12 at 09:34
  • @trojanfoe:Thanks,I did't know how to do that.Please provide some more guideline to update my local iPhone sqlite database from MYSQL server. – Nitin Mar 12 '12 at 12:51

2 Answers2

7

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.

Devang
  • 11,258
  • 13
  • 62
  • 100
  • Thanks.It's work.How can we see this "Database.sqlite" file? Is it possible to add this file in my Xcode group folder? – Nitin Mar 12 '12 at 10:01
  • @Nit : It will be downloaded at App's "Document" directory. You can verify this by enabling "iTunes File Sharing". – Devang Mar 12 '12 at 10:06
  • 1
    Once it is downloaded to documents folder, you can access it directly from the documents folder. Why do you want to add it to "bundle". Even though if the database is in your bundle, its always good to copy the database to documents folder and then use it. – Satyam Mar 12 '12 at 10:09
  • That's right but for verification and i can also use`if (![fileManager fileExistsAtPath: path]) ` for verification but i want see it like as we can see .h file in our Xcode projects. – Nitin Mar 12 '12 at 10:15
  • @Nit : As I told you. By enabling "iTunes File Sharing" you will able to copy database which will help you to verify database. – Devang Mar 12 '12 at 10:21
  • @Devang:"~/Library/Application Support/iPhone Simulator/YOUR-IOS-VERSION/Applications/UNIQUE-KEY-FOR-YOUR-APP/Documents" iPhone create encrypted file on the above location. – Nitin Mar 12 '12 at 12:48
  • @Nit : Take look at : http://stackoverflow.com/questions/1498342/how-to-decrypt-an-encrypted-apple-itunes-iphone-backup , http://stackoverflow.com/questions/4601347/how-to-use-des-algorithm-to-encrypt-or-decrypt-some-data-in-object-c, http://stackoverflow.com/questions/6016318/encrypt-and-decrypt-algorithm-for-image-in-iphone – Devang Mar 13 '12 at 04:24
  • @Devang, is it possible to downloaded multiple .sqlite files and make coredata use these files and switch to a different file when required. – Priyal Jun 15 '17 at 11:19
  • @Priyal: yes you can download as many .sqlite files as you want. And you can manipulate them for coredata use. By switching you mean to use different database, if so then you can create multiple objects of database and use them to fetch required data. – Devang Jun 16 '17 at 06:15
  • @Devang by switching I mean to replace the existing core data file with a new file or make managed object context point to some other .sqlite file. Is it possible? if Yes, Can you please explain with a code sample ? – Priyal Jun 16 '17 at 08:16
0

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.

shripad20
  • 848
  • 1
  • 7
  • 15
  • I have a mysql database which is in my web server. How can I download that database as sqlite database in my app? – Natasha Dec 28 '15 at 20:14
  • @natasha: Sorry for late reply, to download sqlite from web server, you can refer to previous answer by Devang. – shripad20 Jan 14 '16 at 04:31