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 > iPhone SDK Development Forums > iPhone SDK Tutorials > Tutorial Requests

Reply
 
LinkBack Thread Tools Display Modes
Old 07-04-2009, 03:07 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 180
Default Tutorial for saving a data-file in SQLite3

Hi

May I request someone to create a tutorial that shows how to save a data file (image, sound, text etc.) in SQLite3 database?


If there is one already, please point me to it.

Thanks in advance.
Sam
shnaps is offline   Reply With Quote
Old 07-04-2009, 09:38 AM   #2 (permalink)
.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
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: 924
14 members and 910 guests
7twenty7, ala2005, apatsufas, astalavista, davidcoderlab, donlucacorleone, JordanCahill, leticiatroutman, lzwasyc, marshusensei, marWIN77, nikhil, TheWebWizz, watermelonvodka
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,478
Threads: 89,092
Posts: 380,125
Top Poster: BrianSlick (7,091)
Welcome to our newest member, marWIN77
Powered by vBadvanced CMPS v3.1.0

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