Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone ($4.99)

dotnetIQ ($4.99)

Your First iPhone App ($1.99)

iPocket Tools 9 in 1 ($0.99)

Catch-Me (Free)

Alien Strike ($0.99)

Historic Olympic Medal-Table ($1.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: 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, 09: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


» Advertisements
» Stats
Members: 21,496
Threads: 35,779
Posts: 156,743
Top Poster: smasher (2,448)
Welcome to our newest member, GarLe722
Powered by vBadvanced CMPS v3.1.0

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