Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 07-04-2009, 09:38 AM   #2 (permalink)
mr-sk
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Default

Off the top of the head, so there could be errors:

1. You need to include libsqlite3 in your frameworks dir
2. Include sqlite3.h in your controller
3. Make the db writable by copying to your app sandbox
Code:
    NSString *dbFilePath;
    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentFolderPath = [searchPaths objectAtIndex:0];
    dbFilePath = [documentFolderPath stringByAppendingPathComponent:DATABASE_NAME_EXT];
    NSLog(dbFilePath);
    if (![[NSFileManager defaultManager] fileExistsAtPath:dbFilePath])
    {
        // copy the db
        NSString *backupDbPath = [[NSBundle mainBundle] pathForResource:DATABASE_NAME ofType:DATABASE_EXT];
        NSLog(backupDbPath);
        if (backupDbPath == nil)
        {
            NSLog(@"viewDidLoad.backupDbPath is nil");
        }
        else
        {
            BOOL copiedBackupDb = [[NSFileManager defaultManager] copyItemAtPath:backupDbPath toPath:dbFilePath error:nil];
            if (!copiedBackupDb)
            {
                NSLog(@"viewDidLoad.copiedBackupDb is NO");
            }
        }
    }

4. Write to the db (insert/update query)

DATABASE_NAME_EXT & DATABASE_EXT are defined in my .h file for convenience. A NSString will do.

Code:
-(void)insertIntoDB:(NSString *)sqlStr
{
    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentFolderPath = [searchPaths objectAtIndex:0];
    NSString *dbFilePath = [documentFolderPath stringByAppendingPathComponent:DATABASE_NAME_EXT];
    
    sqlite3 *dbHandle;
    int dbrc;
    dbrc = sqlite3_open([dbFilePath UTF8String], &dbHandle);
    
    sqlite3_stmt *preparedStatement;
    const char* queryStatement = [sqlStr UTF8String];
    dbrc = sqlite3_prepare_v2(dbHandle, queryStatement, -1, &preparedStatement, NULL);
    dbrc = sqlite3_step(preparedStatement);
    sqlite3_finalize(preparedStatement);
    sqlite3_close(dbHandle);    
}
You'll probably want to add in some error checking as well.
Hope that helps a bit -
mr-sk is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 158,838
Threads: 89,210
Posts: 380,646
Top Poster: BrianSlick (7,129)
Welcome to our newest member, hariky007
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:59 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.