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 > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 03-20-2010, 09:31 AM   #1 (permalink)
nlc
Registered Member
 
Join Date: Aug 2008
Posts: 32
nlc is on a distinguished road
Default replace old SQLite3 database with new but keep parts of content from old db

As the topic says, is there any good pattern for that? As it is now I have a SQLite3 database and want to use couple of columns from the old db in the next version that is a new and bigger db.

For example. old database has a table with animals and a list of properties like "I have seen this animal" stores a value for it. When I replace the old database with a new one that has more information about the animals I want the users profile for the saves to be transfered to the new one. Or something similar...

If someone followed, any solutions or suitable patterns are welcome. As it is now I have thought of different options but none that feels just "right"

Thanks
nlc is offline   Reply With Quote
Old 03-21-2010, 12:03 AM   #2 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
iisword is on a distinguished road
Default

would like to know this too...
__________________
iisword is offline   Reply With Quote
Old 03-21-2010, 05:56 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2009
Age: 25
Posts: 40
Fuzzy Logic is on a distinguished road
Default

Not so sure what's the right way of doing this, but I can tell you how I'm doing it.
For starters, it would be easier for the future to keep a field with the version of the database, because you never know how many times you'll want to change the structure and your update needs to handle what ever version the user upgrades from.

I'm guessing you probably have some sort of a method that checks if the db is in the user directory and if not, it creates a template db (possibly by copying an empty db from the bundle), and then initialize the objects that provide the interface to the db. What I do is, after checking if the db is present, I check for missing columns (and for later versions of my app a db version field), and make the needed changes to the db based and the version of the db I'm updating from.

Here's a small part of my code for example (ugly... I know... it's one of those quick hacks that I never get to rewrite). Notice that I use a general query and check for number of columns in the db, and then I use ALTER TABLE sql command to add another column scrollspeed of type real to table song.

Code:
- (void)checkDatabaseAndCorrectIfNeeded{
int success;
	//check for version 1.1 missing features
	sqlite3_stmt *checkColumns;
	const char *sqlCheckColumns = "SELECT * FROM song";
	if (sqlite3_prepare_v2(database, sqlCheckColumns, -1, &checkColumns, NULL) == SQLITE_OK){
		if (sqlite3_column_count(checkColumns) <= 6){
			//Update version 1.1 changes
			sqlite3_stmt *addScrollSpeedColumn;
			const char *sqlAddScrollSpeedColumn = "ALTER TABLE song ADD scrollspeed REAL";
			if (sqlite3_prepare_v2(database, sqlAddScrollSpeedColumn, -1, &addScrollSpeedColumn, NULL) == SQLITE_OK) {
				// Execute the query.
				success = sqlite3_step(addScrollSpeedColumn);
				// Handle errors.
				if (success != SQLITE_DONE) {
					NSAssert1(0, @"Error: failed to alter the database with message '%s'.", sqlite3_errmsg(database));
				}
				sqlite3_finalize(addScrollSpeedColumn);
			}
....
(Obviously it's not all of it...)

I hope that helps a bit... there's probably a better (and cleaner) way to do that...
Fuzzy Logic is offline   Reply With Quote
Reply

Bookmarks

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: 354
7 members and 347 guests
.Snipe, BSH, givensur, guusleijsten, NSString, Paul Slocum, SillyHoney
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,645
Threads: 94,111
Posts: 402,862
Top Poster: BrianSlick (7,990)
Welcome to our newest member, leighec68
Powered by vBadvanced CMPS v3.1.0

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