views:

486

answers:

1

Hello Every1,

i maintain a sqlite db in my iPhone app so, i was wondering how u could persist a single version and updated one.

its said that if want to make updates to the db u should copy it to the documents folder

on my iPhone simulator the documents folder changes like everytime i open the app

if its copied and changes was made .. how come the app reflect the updates to the db in my app bundle so when the app is up again the app copies the db again from my bundle to the documents folder to be able to make changes to it..

my problem is that sometimes i make changes and load the app again to find that the changes are gone!

all what i have done:

1-created the db using terminal on a certain path and on resources folder in xcode right clicked add existing file 2-i do this each time i load the app : -(void)DBinit { // Setup some globals databaseName = @"articlesdb.sql";

// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [[documentsDir stringByAppendingPathComponent:databaseName] copy];
NSLog(databasePath);
// Execute the "checkAndCreateDatabase" function
[self checkAndCreateDatabase];

}

-(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) 
{
 [fileManager removeItemAtPath:databasePath error:nil];
 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];

[fileManager release];

}

appreciate your help

+1  A: 

Xcode, in conjunction with the iPhone Simulator, creates a new app folder for each build of your app and will move the the old documents folder and its contents over to the new app folder. It is up to you to check the contents of the documents folder and decide whether a file you have in your Bundle is preferable for use over the one that is in the documents folder. You should make this decision wisely and not lightly.

This is the right behavior. The user of your software may have made some significant changes and modifications to the database. They probably would not like it if, when upgrading for a minor bug fix or even for feature enhancements, their database changes, their high scores, their modified images, or whatever your app saves, suddenly gets replaced with a generic version.

What you should do is to compare the data in your new database, vs the data the user has, and in the case of SQLite, your should import or copy the user's entry into your new database, and then you can move your new database over to the documents folder, removing the user's but replacing it with one that has the user's data and your new data.

In short: make sure you don't replace an existing database with a new one from your bundle, since that will blow away the user's changes.

mahboudz
thanks for help and timewhat i want to do, is after the user make changes to the database in the documents folder.i want it to be copied ( with changes made ) to the database in the bundle ..
zanque
or that happens automatically
zanque
The Bundle is always read-only. Nothing can be copied back into it. That is why you have to copy databases to the Downloads folder, otherwise the database is read-only.
mahboudz