Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.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, 02:07 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 40
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, 08:38 AM   #2 (permalink)
.38 special tucked
iPhone Dev SDK Supporter
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 132
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


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,318
Threads: 39,113
Posts: 171,502
Top Poster: smasher (2,576)
Welcome to our newest member, lerner274
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:31 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0