views:

393

answers:

1

Hi guys: Searched on here and got some vague answers, so I thought i'd rephrase the problem to get some clearer answers-

Right now I have an SQL Lite db that reads/parses information from a pre-formatted .txt file. When I open the app, there is a slight 'lag' as the iDevice parses the info, then gets fetched for the iDevice. I'm just wondering if there's any way to just 'save' all the information directly in the xCode so there's no lag/fetch time?

Thank you.

A: 

What you can do is pre-build your sqlite database and then include it as a resource in your application. As this database is inside your application bundle it will be read only on your device so you will need to make a copy of it in your application document area.

I have successfully used something like the following code in a production iPhone application

NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolderPath = [searchPaths objectAtIndex: 0];

NSString* dbFilePath = [documentFolderPath stringByAppendingPathComponent: DATABASE_FILE_NAME]; 


if(![[NSFileManager defaultManager] fileExistsAtPath: dbFilePath]) {
    // copy the template database into the right place
    NSError* error = nil;
    [[NSFileManager defaultManager] copyItemAtPath: [[NSBundle mainBundle] pathForResource: @"template" ofType: @"db"] toPath: dbFilePath error: &error];
    if(error) {
        @throw [NSException exceptionWithName: @"DB Error" reason: [error localizedDescription] userInfo: nil];
    }
}

int dbrc; // database return code
dbrc = sqlite3_open ([dbFilePath UTF8String], &db);
if(IS_SQL_ERROR(dbrc)) {
    @throw [NSException exceptionWithName: @"DB Error" reason: NSLocalizedString(@"Could not open database", @"Database open failed") userInfo: nil];
}
Daniel
If you only need to read information from the db, there is no need to copy the file to the read/write area. SQLite is perfectly capable of opening a db in read-only mode.
Claus Broch
Nice, thanks Daniel!
Justin