SQLite读写二进制数据


- (MRecTemplate*)GetDataRecTemplate
{
    MRecTemplate* data = [[[MRecTemplate alloc] init] autorelease];

    sqlite3_stmt* stmt;
    NSString* str = @"SELECT * FROM RecTemplates";
    int nResult = sqlite3_prepare_v2(m_db, [str UTF8String], -1, &stmt, NULL);
    if (nResult == SQLITE_OK)
    {
        if (sqlite3_step(stmt) == SQLITE_ROW)
        {
            int i = 0;
            char* Disclaimer = (char*)sqlite3_column_text(stmt, i++);
            data.Disclaimer = Disclaimer ? [NSString stringWithUTF8String:Disclaimer] : @"";
            data.Logo = [[[NSData alloc]
                         initWithBytes:sqlite3_column_blob(stmt,i) length:sqlite3_column_bytes(stmt,i++)]
                         autorelease];
            char* DateModified = (char*)sqlite3_column_text(stmt, i++);
            data.DateModified = [m_dfDateTime dateFromString:[NSString stringWithUTF8String:DateModified]];
            char* ServerTime = (char*)sqlite3_column_text(stmt, i++);
            data.ServerTime = [m_dfDateTime dateFromString:[NSString stringWithUTF8String:ServerTime]];
        }
        sqlite3_finalize(stmt);
    }
    else
    {
        LOG(@"Problem in DB, return=%d, SQL=%@", nResult, str);
    }

    return data;
}

- (BOOL)AddDataRecTemplate:(MRecTemplate*)data
{
    if (data.ServerTime == nil)
        data.ServerTime = [NSDate date];

    sqlite3_stmt* stmt;
    NSString* str = [NSString stringWithFormat:
                     @"INSERT INTO RecTemplates(Disclaimer, Logo, ServerTime) VALUES('%@', ?, '%@')",
                     data.Disclaimer,
                     [m_dfDateTime stringFromDate:data.ServerTime]];
    int nResult = sqlite3_prepare_v2(m_db, [str UTF8String], -1, &stmt, NULL);
    if (nResult == SQLITE_OK)
    {
        if (data.Logo)
            sqlite3_bind_blob(stmt, 1, [data.Logo bytes], [data.Logo length], NULL);
        else
            sqlite3_bind_blob(stmt, 1, nil, -1, NULL);
        sqlite3_step(stmt);
        sqlite3_finalize(stmt);
    }
    else
    {
        LOG(@"Problem in DB, return=%d, SQL=%@", nResult, str);
        return NO;
    }

    return YES;
}

- (BOOL)UpdateDataRecTemplate:(MRecTemplate*)data
{
    if (data.ServerTime == nil)
        data.ServerTime = [NSDate date];

    sqlite3_stmt* stmt;
    NSString* str = [NSString stringWithFormat:
                     @"UPDATE RecTemplates SET Disclaimer='%@', Logo=?, ServerTime='%@'",
                     data.Disclaimer,
                     [m_dfDateTime stringFromDate:data.ServerTime]];
    int nResult = sqlite3_prepare_v2(m_db, [str UTF8String], -1, &stmt, NULL);
    if (nResult == SQLITE_OK)
    {
        if (data.Logo)
            sqlite3_bind_blob(stmt, 1, [data.Logo bytes], [data.Logo length], NULL);
        else
            sqlite3_bind_blob(stmt, 1, nil, -1, NULL);
        sqlite3_step(stmt);
        sqlite3_finalize(stmt);
    }
    else
    {
        LOG(@"Problem in DB, return=%d, SQL=%@", nResult, str);
        return NO;
    }

    return YES;
}

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google+ photo

You are commenting using your Google+ account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d 博主赞过: