Nithin S
Nithin S

Reputation: 109

What are the methods access sqlite db directly from bundle path in iphone sdk?

Is it ok to access sqlite db directly from bundle path OR Copy sqlite db from bundle to documentary path and then access sqlite db? Which is efficient method? Can anyone please suggest me on this?

Upvotes: 0

Views: 623

Answers (3)

Santosh Gurram
Santosh Gurram

Reputation: 1017

If you want to access or read the DB data directly from the documents directory,

  1. You have to copy the DB to documents directory programmatically and then you can use the below code to access the DB (Select,Update,Delete operations).

    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES) objectAtIndex:0];
    
    NSString* foofile = [documentsPath stringByAppendingPathComponent:@"XYZ.sqlite"];
    
    
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
    
    if(fileExists)
    {
     if (foofile)
     {
       myText = [[NSString alloc] initWithContentsOfFile:foofile];    
       if (myText)
       {
        [self start];
       }
       else{;}      
     }
    }
    else
    {
      NSString *filePath = [[NSBundle mainBundle] pathForResource:@"XYZ" 
                                                           ofType:@"sqlite"];
      if (filePath)
      {  
       myText = [[NSString alloc] initWithContentsOfFile:filePath];    
       if (myText)
       {
          [self start];
       }
       else
       {;}      
      }
     }
    

Upvotes: 0

the monk
the monk

Reputation: 479

It would be better for you to copy the DB to the Library or document Directory, and make all the process from the locally saved database in document or library folder.

I prefer to keep it to library folder such that user can not open and delete it , just for safety. If u access the DB from the Main bundle you can have a problem, further if you upgrade the app version and want to make changes to your DB.

So, better option is to keep the DB to Document or library folder and access it from there.

Hope it makes some sense.

Upvotes: 0

Himanshu A Jadav
Himanshu A Jadav

Reputation: 2306

You can not change the files that are in bundle. If you need to change in the database you need to copy it in Documents directory. but if the database is not being changed you may keep it in bundle. But it is convention that people create copy of database in Documents directory.

Change in database = manipulate records of database.

Upvotes: 1

Related Questions