1

Hi I'm planning to add a static SQLite3 from my projects resource folder but I dont know if I'm doing it correctly.

so far here's my progress... Created SQLite3 Database from SQLite Database Browser

Copied Sqlite3 Database to my project folder enter image description here

Added network.db to my project resource folder enter image description here

and now I don't know where to go next.. I hope someone can help me with my problem.

RonPelayo
  • 376
  • 5
  • 22
  • Where do you want to add your .db file ? in the documents directory ? – NKB Apr 23 '14 at 06:33
  • You need to copy this to the documents folder before you do any operations on DB. – Pooja M. Bohora Apr 23 '14 at 06:33
  • you should copy this to document directory for inserting , deleting or updating data , because you can't access directory from mainbundle – Pandey_Laxman Apr 23 '14 at 06:33
  • check out my answer on this [link](http://stackoverflow.com/questions/14122055/sqlite-database-creation-in-iphone-sdk/14122102#14122102) . this will `copy your database into document directory file from your bundle`. then you can use this database and insert , update , delete your data. – ChintaN -Maddy- Ramani Apr 23 '14 at 06:39
  • possible duplicate of [How to copy sqlite database when application is launched in iOS?](http://stackoverflow.com/questions/15491238/how-to-copy-sqlite-database-when-application-is-launched-in-ios) – Leena Apr 23 '14 at 06:42
  • I'm planning to get my initial data in my networks.db, basically it just means that it will only run when the app first launches. – RonPelayo Apr 23 '14 at 06:45

3 Answers3

4

If you are modifying your database at run time (i.e. you are inserting, deleting or updating records), you need to create a copy from bundle to documents directory. iOS sendboxing mechanism will not allow you to modify bundle resource at run time. So, you need to copy it to documents directory. Below code can help you out:

 NSFileManager *fileManager = [NSFileManager defaultManager];
 NSError *error = nil;

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
 NSString *documentsDir = [paths objectAtIndex:0];
 NSString *filePath = [documentsDir stringByAppendingPathComponent:@"network.db"];

 BOOL success = [fileManager fileExistsAtPath:filePath];

 if(!success)
 {
      NSString *defaultDBPath = [[[NSBundle mainBundle] pathForResource:@"network" ofType:@"db"];
      success = [fileManager copyItemAtPath:defaultDBPath toPath:filePath error:&error];

      if (!success)
           NSAssert1(0, @"Failed to create writable resource file with message '%@'.", [error localizedDescription]);

 }

If you are referring your db for lookup purpose only (only using it for select queries), there is no need to copy from bundle to documents directory.

Apurv
  • 17,116
  • 8
  • 51
  • 67
1

You need to add that project file into the documents directory,

You can do this for that,

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *sourcePath = [[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:@"networks.db"];

NSError *error;

[[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:documentsDirectory error:&error];

And then if you want to do anything with the SQLite3 file, you can use any SQLite Managers, For example SQLite Manager

NKB
  • 651
  • 5
  • 13
1

check out my answer on this link . this will copy your database into document directory file from your bundle. then you can use this database and insert , update , delete your data.

ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48