Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-26-2009, 12:03 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Location: California
Posts: 54
rooster117 is on a distinguished road
Default What happens to a user's database when you submit a new version.

Hello,

I am working on an app which will include an sqlite3 database. I am curious as to what happens when a new version of the app gets installed on the user's phone. Does the user's data remain? If I needed to add new fields to existing tables or new tables altogether does that force a reinstall of the database?

Thanks for any help you can provide.

-Ryan

-----------------

Bolt and Nut iphone utility app

Tooth Brush Timer
rooster117 is offline   Reply With Quote
Old 04-26-2009, 12:20 AM   #2 (permalink)
Registered Member
 
brendand's Avatar
 
Join Date: Aug 2008
Posts: 223
brendand is on a distinguished road
Default

Your database remains intact. But if you want to upgrade your database, make sure you do so in such a way that you can upgrade users who have older versions than just your previous one. Not everyone will update to each new version you release.

I use "pragma user_version" to determine the current database schema version. Then I update the database, and then increment the user_version (pragma user_version = [your new version].

Unfortunately with SQLite, you can't delete columns. Who ever came up with that brain dead idea to not allow deleting of columns should be shot. So that makes fixing up past model mistakes complicated because you either have to just leave the column there and add a new column, or migrate your data to a new table that doesn't have the old column. Ugh.

Anyway, just thought I'd get that off my shoulders. :-)

I use FMDB for my SQLite database access, so this code may not work with your setup, but it might point you in the right direction on how to implement it for your needs:

Code:
+ (void)executeSqlFileAtPath:(NSString *)path forDatabase:(FMDatabase *)database {
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSError *error;
	
	if ([fileManager fileExistsAtPath:path]) {
		NSString *sql = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
		// loop through lines in string;
		if (sql != nil) {
			NSArray *sqlStatements = [sql componentsSeparatedByString:@";\n"];
			for (NSString *sqlStatement in sqlStatements) {
				BOOL success = [database executeUpdate:[sqlStatement stringByAppendingString:@";"]];
			}
		}
		[sql release];
	}
}

// migration support so we can upgrade the database when new features come out.
+ (BOOL)migrateDatabase:(FMDatabase *)database {
	BOOL success = YES;
	// get current database version of schema
	int currentVersion = 0;
	int databaseVersion = [database intForQuery:@"pragma user_version;"];
	if (databaseVersion) {
		currentVersion = databaseVersion;
	}
	
	NSString *migrationFilePath = nil;

	// get latest database version number from Info.plist distributed with app
	int latestVersion = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"TFDatabaseVersion"] intValue];
	
	// if current version is older than latest version, then perform migration
	// read all migration sql files from current version + 1 to latest version
	// execute sql from each file in order.

	if (currentVersion < latestVersion) {
		[[NSNotificationCenter defaultCenter] postNotificationName:kMigratingDatabaseNotification object:NULL];
	}
	
	for (int i = currentVersion + 1; i <= latestVersion; i++) {
		migrationFilePath = [NSString stringWithFormat:@"%@/migration.%i.sql", [[NSBundle mainBundle] resourcePath], i];
		// load the sql file into memory and execute each sql block
		[DBUtils executeSqlFileAtPath:migrationFilePath forDatabase:database];
	}
	
	if (currentVersion < latestVersion) {
		[database executeUpdate:[NSString stringWithFormat:@"pragma user_version = %i;", latestVersion]];
		[[NSNotificationCenter defaultCenter] postNotificationName:kMigratingDatabaseCompleteNotification object:NULL];
	}
	return success;
}
Oh ya, I store all my database migrations in files called migration.0.sql, migration.1.sql, ... migration.n.sql. I load them in one by one and execute the sql contained within them. Works really well.


Hope this helps a bit.
brendand is offline   Reply With Quote
Old 04-26-2009, 09:38 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2009
Location: California
Posts: 54
rooster117 is on a distinguished road
Default That helps a lot

Thank you for the reply. That helps a lot and I had a feeling it was something like that. I'm gonna make sure to put in a lot of work to ensure my tables are correct before the first release but that migration makes a lot of sense.

I really appreciate it the help!

-Ryan
rooster117 is offline   Reply With Quote
Reply

Bookmarks

Tags
database, sql, updates

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 314
7 members and 307 guests
blueorb, givensur, guusleijsten, jbro, mer10, n00b, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:00 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0