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 Tools & Utilities

Reply
 
LinkBack Thread Tools Display Modes
Old 01-12-2010, 03:26 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 26
borg359 is on a distinguished road
Default How do you keep the SQLite database open for the entire app session?

Hi everyone,
I'm trying to improve my SQLite performance in a mapping application which issues a large number of db queries as the user scrolls around the map. I've read several post which suggest keeping the database open for the entire application session, rather than opening and closing the db as needed, is one method to improve such queries. To this end, I've moved my sqlite3_open command into the applicationDidFinishLaunching call, but the problem is that my annotation queries are made in another view controller in which the database file has not been defined.

So my question is this: How do you pass along the database information that is generated in the sqlite3_open command to the rest of your application. Sqlite3 * database is not an NSObject, so it can't be passed from the rootviewcontroller to subsequent view controllers as a property of those views. Any suggestions on how this is done? Below is the relevant code showing what I'm trying to do now. That setup currently results in a EXEC_BAD_ACCESS error, presumably because database is undefined.

Anyway, I'm assuming it's something extremely easy that I'm missing so any tips would be greatly appreciated. Thanks!

Some Code:

myAppDelegate.h
Code:
#import "myAppDelegate.h"
#import "sqlite3.h"

- (void)applicationDidFinishLaunching:(UIApplication *)application { 

	// Create the database file name
	NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
	NSString * documentsDir = [paths objectAtIndex:0];
	NSString * file = [documentsDir stringByAppendingPathComponent:@"Markers.db"];	
	//	NSString * file = [documentsDir stringByAppendingPathComponent:@"Map2.db"];	
	
	NSLog(@"Loading database file %@", file);
	
        sqlite3 *database = NULL;
        if (sqlite3_open([file UTF8String], &database) == SQLITE_OK) {
		NSLog(@"Database Open!");
	}

mapViewController.m

Code:
#import "mapViewController.h"
#import "myAppDelegate.h"

// Defining the SQL callback functions
static int NameCallback(void *context, int count, char **values, char **columns)
{
    NSMutableArray *Name = (NSMutableArray *)context;
    int i;
	for (i=0; i < count; i++) {
        const char *nameCString = values[i];
//		NSLog(@"Saved Title = %s", nameCString);
		
		// Protecting against special characters
		if ([NSString stringWithUTF8String:nameCString] == nil) { 
			[Name addObject:@"Frak"];
		} else {
			[Name addObject:[NSString stringWithUTF8String:nameCString]];
		}
	}
    return SQLITE_OK;
}

@implementation mapViewController

- (void)viewDidLoad {

   //load from nib, blah blah...
   [self loadNamesFromDatabase];
}

- (void)loadNamesFromDatabase {

     sqlite3_exec(database,"select name from Markers", NameCallback, names, NULL);

}

}
-dan
borg359 is offline   Reply With Quote
Old 01-20-2010, 08:29 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Why do you think you can't pass the sqlite3 * to your view controllers? You can... you just can't "retain" it, but that's OK. So you just need to omit "retain" from your property declaration.

Although I would probably just make your sqlite3 *database variable an instance variable of your AppDelegate class (which you need to do anyway) and have the view controllers ask the app delegate for the database.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Reply

Bookmarks

Tags
close, open, performance, sql

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: 376
8 members and 368 guests
comicool, husthlj, illogical, LegionMD, LunarMoon, mer10, Murphy, padsoftware
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,677
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, husthlj
Powered by vBadvanced CMPS v3.1.0

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