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 08-05-2010, 02:49 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 5
antifreeze is on a distinguished road
Default How to load a sqlite file that had already prepared?

i want to use a sqlite file just read-only. but when i build and run my project, my app create a new db file even there my file already prepared.

so, i explored iphone simulator directory and i saw that my app didn't copy my prepared sqlite file to 'myapp\document'.

what should i do?
antifreeze is offline   Reply With Quote
Old 08-05-2010, 04:10 AM   #2 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 40
TSurridge is on a distinguished road
Default

Quote:
Originally Posted by antifreeze View Post
i want to use a sqlite file just read-only. but when i build and run my project, my app create a new db file even there my file already prepared.

so, i explored iphone simulator directory and i saw that my app didn't copy my prepared sqlite file to 'myapp\document'.

what should i do?
in your appDelegate .m file:


Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
	
	//**************** Check if Database is on Users Phone. If not move a copy over ********************
	
	databaseName = @"putyourDBNameHere.sqlite";
	
	// 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];
	
	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
	if(success) {
		NSLog(@"Database already exists.");
	}
	else {
		// If not then proceed to copy the database from the application to the users filesystem
		NSLog(@"Database does not exist. *** Moving one from mainBundle ***");
		NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
		// Copy the database from the package to the users filesystem
		[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
	}
	
    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    return YES;
}

Last edited by TSurridge; 08-05-2010 at 04:15 AM.
TSurridge is offline   Reply With Quote
Old 08-05-2010, 04:14 AM   #3 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 40
TSurridge is on a distinguished road
Default

.h file you will need

#import <sqlite3.h>

....
....

// Database variables
NSString *databaseName;
NSString *databasePath;
sqlite3 *database;
TSurridge 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: 322
10 members and 312 guests
alexP, arash5500, gordo26, mediaspree, nobstudio, Objective Zero, rayjeong, Sloshmonster, stanny, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

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