Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

boyplunder

macrumors regular
Original poster
Sep 8, 2008
165
0
UK
Hi All,

I have a couple of apps that I'm working on that use the same core table view code. I'm using a method called 'checkAndCreateDatabase', which I have found many people are using. One of the apps I'm completing needs to ONLY use the data file in the bundle, but I can't for the life of me see what the change is to stop it having to copy the data file to the documents folder.

- (void)checkAndCreateDatabase {
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;

// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];

// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:databasePath];

// If the database already exists then return without doing anything
if (success) return;

// If not then proceed to copy the database from the application to the users filesystem

// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

}

The data will be updated regularly, but there should only be one and not in the documents folder. If anyone can point me in the right direction I will be eternally greatful.
 
Your SQLite file must be outside the bundle folder. If you modify it while it's in the bundle folder, your application's signature will be invalid. The only remaining options are the Documents folder - which is unique to your application and the Library folder - which is also unique to your program. I'd recommend putting it into the documents folder if the database pertains to the working data for your application.
 
Thanks for responding.

The point is that this app will only display the data, which will be searchable, but not editable. I don't want it to copy the database file, just use it.

If the file is copied to the documents folder, the app registers it as already copied, so any updated datafile will be ignored. I have found that if I change the datafile name each update [for example, if the first update was named database1], it automatically updates the app, but that means there will be multiple datafiles copied for the app. All but the latest will just sit there wasting space. This doesn't seem to me like good housekeeping.

What does make sense to me is that I just refer to the datafile in the bundle.

If it MUST be outside the bundle to work, an alternative method, again with a bit of help maybe, would be to copy the file to documents EVERY time the app is opened. Not a big datafile, so that's a possibility.

Any takers?
 
Thanks for responding.

The point is that this app will only display the data, which will be searchable, but not editable. I don't want it to copy the database file, just use it.

If the file is copied to the documents folder, the app registers it as already copied, so any updated datafile will be ignored. I have found that if I change the datafile name each update [for example, if the first update was named database1], it automatically updates the app, but that means there will be multiple datafiles copied for the app. All but the latest will just sit there wasting space. This doesn't seem to me like good housekeeping.

What does make sense to me is that I just refer to the datafile in the bundle.

If it MUST be outside the bundle to work, an alternative method, again with a bit of help maybe, would be to copy the file to documents EVERY time the app is opened. Not a big datafile, so that's a possibility.

Any takers?

Take a look at sqlite3.h. More specifically sqlite3_open(), sqlite3_exec(), and sqlite3_close(). To use these you'll need to implement a call back function.

But, as Menge pointed out, you cannot make changes to this database when it resides in your bundle. That includes any type of updates which you refer to by saying "The data will be updated regularly." Apple code-signs the bundle that you provide to them at the time of submission and changes to that bundle cannot be made.
 
The purpose of your checkAndCreateDatabase method is to copy the database file from your app bundle to the Documents folder if there isn't already a copy in the Documents folder. The reason to do this is because you can't modify a file inside the app bundle. If you don't intend to modify the database file then you don't need to use this method. You can just read the database file from the app bundle.

Regarding updates I add a version to my database file. When opening the database file in the Documents folder my code checks the version number. If it's not the current version number the code then 'does the right thing.'
 
Thats right, I needed to do exactly what you want. Non editable desire needn't be anywhere else but in the app bundle itself.

The method you created needn't be used at all.

A good example is the SQLiteBooks.

method - initialiseDatabase is sufficient.

Code:
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.lite"];
if(sqlite3_open([path UTF8String], db) ==SQLITE_OK) { 
do your stuff
}

I'm still learning. This is the basic code change. I wouldnt really know if there was a better way.

However, How advisable is it to do it this way? Specially when the DB is meant to be readonly.

thanks
 
One of the first apps I looked into was the SQLiteBooks example, because it was the nearest I could get to what I had in mind. I am a Noob, let's get that straight, and I decided to take my time to work through and learn.

In saying that, I think the SQLiteBooks example is actually quite complicated. Too complicated for a concept example. I have since learnt other ways and methods that are less complicated, but it didn't instill me with confidence at the time.

Thank you for the comments and I'll go and try some of the points here.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.