您的位置:首页 > 数据库

iOS开发15-iOS SQLite存取图片、视频、音频

2015-11-01 16:13 621 查看

iOS开发15-iOS SQLite存取图片、视频、音频

代码下载(Xcode7.0.1)

有问题请联系博主,邮箱:nathanlee1987@aliyun.com

在实际开发中有时候需要使用数据库存储图片、视频和音频数据。这个时候就需要将图片、视频音频数据转换成NSData,然后上传到数据库中。

读取数据的时候要从数据库获取这些NSData数据,然后再转换成应有的格式,存入到本地文件夹。

1、设计数据库

数据库的表的结构:m_data 类型是blob用来存储NSData二进制数据



建表语句:

CREATE TABLE myTable(m_id integer primary key autoincrement not null,m_name text,m_type text,m_time text,m_data blob)

2、上传图片、视频、音频到数据库

向数据库中添加数据的方法:

//添加图片、音乐、视频(数据类型、数据名、数据)
-(void)insertWithDataName:(NSString*)dataName DataType:(NSString*)dataType data:(NSData*)data{

[self openDB];
NSString *sqlString=@"insert into myTable (m_name ,m_type,m_data,m_time) values (?,?,?,?)";

sqlite3_stmt *stmt=NULL;

int result = sqlite3_prepare(db, sqlString.UTF8String, -1, &stmt, NULL);

if (result==SQLITE_OK) {

NSLog(@"预执行正确");

//绑定参数
//第1个问号
sqlite3_bind_text(stmt, 1, dataName.UTF8String, -1, NULL);
//第2个问号
sqlite3_bind_text(stmt, 2, dataType.UTF8String, -1, NULL);
//第3个问号
sqlite3_bind_blob(stmt, 3, [data bytes], (int)[data length], NULL);
//第4个问号
sqlite3_bind_text(stmt, 4, [self getNowDate].UTF8String, -1, NULL);

//执行
if(sqlite3_step(stmt)==SQLITE_DONE)
NSLog(@"插入成功");
else NSLog(@"插入失败");

}
else NSLog(@"预执行错误");

sqlite3_finalize(stmt);
[self closeDB];

}


调用方法添加数据:

//将数据写入到数据库
-(void)button2Action:(UIButton*)sender{
NSLog(@"添加数据");

NSBundle *mainBundle =[NSBundle mainBundle];

NSLog(@"添加 芦田爱菜.png");
NSString *picPath = [mainBundle pathForResource:@"芦田爱菜" ofType:@"png"];
NSData *dataPic = [[NSData alloc]initWithContentsOfFile:picPath];

if (![[MyDataBaseHandle shareDatabase]checkWithDataName:@"芦田爱菜" DataType:@"png"]) {
[[MyDataBaseHandle shareDatabase] insertWithDataName:@"芦田爱菜" DataType:@"png" data:dataPic];
}
else {
[[MyDataBaseHandle shareDatabase] updateWithDataName:@"芦田爱菜" DataType:@"png" data:dataPic];
}

NSLog(@"添加 平凡之路-片段.mp3");
NSString *MP3Path = [mainBundle pathForResource:@"平凡之路-片段" ofType:@"mp3"];

NSData *dataMP3 = [[NSData alloc]initWithContentsOfFile:MP3Path];

if (![[MyDataBaseHandle shareDatabase]checkWithDataName:@"平凡之路-片段" DataType:@"mp3"]) {
[[MyDataBaseHandle shareDatabase] insertWithDataName:@"平凡之路-片段" DataType:@"mp3" data:dataMP3];
}
else {
[[MyDataBaseHandle shareDatabase] updateWithDataName:@"平凡之路-片段" DataType:@"mp3" data:dataMP3];
}

NSLog(@"添加 SNH 赵粤.mp4");
NSString *MP4Path = [mainBundle pathForResource:@"SNH 赵粤" ofType:@"mp4"];
NSData *dataMP4 = [[NSData alloc]initWithContentsOfFile:MP4Path];

if (![[MyDataBaseHandle shareDatabase]checkWithDataName:@"SNH 赵粤" DataType:@"mp4"]) {
[[MyDataBaseHandle shareDatabase] insertWithDataName:@"SNH 赵粤" DataType:@"mp4" data:dataMP4];
}
else {

[[MyDataBaseHandle shareDatabase] updateWithDataName:@"SNH 赵粤" DataType:@"mp4" data:dataMP4];
}

}


3、下载图片、视频、音频,并存入本地文件夹

从数据库获取数据的方法:

//查
-(NSData *)selectWithDataName:(NSString*)dataName DataType:(NSString*)dataType{

[self openDB];
NSData * data = nil;
NSString *sqlString=@"select m_data from myTable where m_name=? and m_type = ?";

//2.伴随指针
sqlite3_stmt *stmt=NULL;

//3.预执行
int result = sqlite3_prepare(db, sqlString.UTF8String, -1, &stmt, NULL);

if (result==SQLITE_OK) {

NSLog(@"语句正确");

//4.绑定参数
sqlite3_bind_text(stmt, 1, dataName.UTF8String, -1, NULL);
sqlite3_bind_text(stmt, 2, dataType.UTF8String, -1, NULL);

//5.执行(N次,因为有可能有N条数据符合条件)//循环判断是否还有符合查询条件的数据
while (sqlite3_step(stmt)==SQLITE_ROW) {

data =[[NSData alloc] initWithBytes:sqlite3_column_blob(stmt, 0) length: sqlite3_column_bytes(stmt, 0)];

}

}
else NSLog(@"语句错误");

//6.关闭伴随指针
sqlite3_finalize(stmt);
[self closeDB];
return data;
}


调用方法,并将数据存储到本地文件夹:

//从数据库获取数据-并存储到本地文件夹
-(void)button3Action:(UIButton*)sender{

NSString * destDateString =[self getNowDate];

NSLog(@"搜索数据");

////获取png图片并写到本地
NSData * data2=[[MyDataBaseHandle shareDatabase] selectWithDataName:@"芦田爱菜" DataType:@"png"];

UIImage *image2 = [UIImage imageWithData:data2];
[self.myIMV setImage:image2];

NSString  *filePic=  [self.filesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@芦田爱菜.png",destDateString]];
[data2 writeToFile:filePic atomically:YES];

//获取Mp3并写到本地
NSData * data3=[[MyDataBaseHandle shareDatabase] selectWithDataName:@"平凡之路-片段" DataType:@"mp3"];

NSString  *filemp3=  [self.filesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@平凡之路-片段.mp3",destDateString]];
[data3 writeToFile:filemp3 atomically:YES];

//获取Mp4并写到本地
NSData * data4=[[MyDataBaseHandle shareDatabase] selectWithDataName:@"SNH 赵粤" DataType:@"mp4"];

NSString  *filemp4=  [self.filesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@SNH 赵粤.mp4",destDateString]];

[data4 writeToFile:filemp4 atomically:YES];

}


程序执行效果:



数据库中写入的数据:



从数据库获取并写入到文件夹中的文件:



代码下载(Xcode7.0.1)

有问题请联系博主,邮箱:nathanlee1987@aliyun.com

著作权声明:本文由http://my.csdn.net/Nathan1987_原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: