首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

数据库写下图片

2012-07-25 
数据库写入图片很简单吧,在UIKit框架里有这个方法[insertSql appendString:BLOB_DATA][insertSql appendS

数据库写入图片
很简单吧,在UIKit框架里有这个方法

[insertSql appendString:BLOB_DATA];
[insertSql appendString:@") VALUES(?1)"];
sqlite3_stmt *insert_statement = nil;
sqlite3 *database = 你的数据库;
if (sqlite3_prepare_v2(database, [insertSql UTF8String], -1, &insert_statement, NULL) == SQLITE_OK) {
    sqlite3_bind_blob(insert_statement, 1, [data bytes], [data length], NULL);
    if(sqlite3_step(insert_statement) != SQLITE_DONE) {
        NSLog(@"Db error %s", sqlite3_errmsg(database));
    }
} else {
    NSLog(@"Db error %s", sqlite3_errmsg(database));
}
sqlite3_finalize(insert_statement);
[insertSql release];



You need to use a BLOB (Binary Large OBject) if you want to store the actual image from that URL.
//Storage
UIImage*image =...;//Retrieve image from URL
NSData*imageData =UIImagePNGRepresentation(image);
sqlite3_bind_blob(compiledStatement,1,[imageData bytes],[imageData length], NULL);

//Retrieval
constvoid*data = sqlite3_column_blob(compiledStatement,1);
int length = sqlite3_column_bytes(compiledStatement,1);
UIImage*retrieved =[UIImage imageWithData:
                          [NSData dataWithBytes:data length:length]];

热点排行