向sqlite数据库插入和读取图片数据 (for ios)
来源:互联网 发布:unity3d 输出 html5 编辑:程序博客网 时间:2024/04/28 19:39
向sqlite数据库插入和读取图片数据 (for ios)
假定数据库中存在表 test_table(name,image), 下面代码将图片文件test.png的二进制数据写到sqlite数据库:
char *name = "test";
NSString * nameString = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
NSString * filePath = [[NSBundle mainBundle] pathForResource:nameString ofType:@"png"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSData * imgData = UIImagePNGRepresentation([UIImage imageWithContentsOfFile:filePath]);
const char * sequel = "insert into test_table(name,image) values(?,?)";
sqlite3_stmt * update;
if (sqlite3_prepare_v2(database, sequel, -1, &update, NULL) == SQLITE_OK)
{
sqlite3_bind_text(update, 1, name, -1, NULL);
sqlite3_bind_blob(update, 2, [imgData bytes], [imgData length], NULL);
if( sqlite3_step(update) == SQLITE_DONE)
{
NSLog(@"已经写入数据");
}
sqlite3_finalize(update);
}
}
else
{
NSLog(@"文件不存在");
}
下面代码从数据库中读取图片二进制数据,然后显示图片:
const char * sequel = "select image from test_table where name=?";
sqlite3_stmt * getimg;
if (sqlite3_prepare_v2(database, sequel, -1, &getimg, NULL) == SQLITE_OK)
{
char *name = "test";
sqlite3_bind_text(update, 1, name, -1, NULL);
if(sqlite3_step(getimg) == SQLITE_ROW)
{
int bytes = sqlite3_column_bytes(getimg, 0);
Byte * value = (Byte*)sqlite3_column_blob(getimg, 1);
if (bytes !=0 && value != NULL)
{
NSData * data = [NSData dataWithBytes:value length:bytes];
UIImage * img = [UIImage imageWithData:data];
UIImageView * aview =[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, IMAGE_WIDTH, IMAGE_HEIGHT)];
aview.image = img;
[self.view addSubview:aview];
[aview release];
}
}
sqlite3_finalize(getimg);
}
- 向sqlite数据库插入和读取图片数据 (for ios)
- sqlite数据库插入和读取图片数据 (for ios)
- iOS sqlite数据库插入和读取图片数据
- 在Sqlite数据库插入和读取图片数据
- 在Sqlite数据库插入和读取图片数据
- 从MySQL数据库读取图片和向数据库插入图片
- 如何向数据库中插入和读取图片2
- 在Java中使用JDBC向数据库插入图片和读取数据库中的图片(八)
- 向数据库插入图片
- php插入图片到sqlite数据库中、从sqlite数据库中读取图片
- 向mysql数据库插入与读取图片文件
- SQLite数据库的数据读取和写入
- Java实现向数据库插入图片和获取数据库图片
- Sqlite数据库及在iOS程序中如何写入和读取数据库数据
- ios sqlite中写入图片二进制数据及读取
- ios sqlite中写入图片二进制数据及读取
- iOS开发 SQLite的创建数据库,表,插入查看数据
- Hibernate向数据库插入图片
- Cocos2d-x学习(十):cocos2d-x 2.0版本中CCScrollView的用法
- 如何在android3.2以上版本中控制横竖屏切换
- Cocos2d-x学习(十一):用cocos2d-x实现MoonWarriors(游戏场景切换和主菜单实现)
- Cocos2d-x学习(十二):用cocos2d-x实现MoonWarriors(无限背景滚动的简单实现)
- socket通过多网卡收发数据
- 向sqlite数据库插入和读取图片数据 (for ios)
- Linux包管理系统
- Cocos2d-x学习(十三):用cocos2d-x实现MoonWarriors(使用触屏改变飞船位置)
- MySQL-Cluster7.2.7安装和配置
- 关于JavaScript中cookie的用法例子
- 多表更新sql语句
- hdu 2865
- [置顶] Cocos2d-x学习(十五):Cocos2d-x内存管理(一)
- oracle总结