开发者

How to directly store SQLite data in XCode?

开发者 https://www.devze.com 2022-12-28 16:32 出处:网络
Searched on here and got some vague answers, so I thought i\'d rephrase the 开发者_JS百科problem to get some clearer answers-

Searched on here and got some vague answers, so I thought i'd rephrase the 开发者_JS百科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.


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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消