一、在Build Phasses 中的Link Binary With Libraries 导入sqlite3.tab
二、使用MVC的框架
1.在model里定义一个属性 格式(字符串)
2.在LoadData.h 中添加数据如下:
(1)先导入头文件:
#import 《sqlite3.h》
#import "Model.h"
(2) 定义全局变量
@interface LoadData : NSObject{
sqlite3 *sqliteDB;
NSData *data;
}
(3)定义样式
3.在LoadData.m 中添加数据如下:
导入static LoadData *ld = nil;
//定义单列类
+(instancetype)sharLoadData{
//定义静态
static dispatch_once_t onecet;
//定义数据
dispatch_once(&onecet, ^{
//初始化
ld = [[LoadData alloc]init];
});
return ld;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
if (!ld) {
ld = [super allocWithZone:zone];
}
return ld;
}
-(id)copy{
return self;
}
-(id)mutableCopy{
return self;
}
//打开数据库
-(void)openvalewLoadData{
//创建沙盒路径
NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
//在沙盒路径下创建一个db文件
NSString *path = [str stringByAppendingPathComponent:@"gege.db"];
if (sqlite3_open([path UTF8String], &(sqliteDB) ) == SQLITE_OK) {
NSLog(@"打开成功");
[self sharvalewLoadData];
}
}
//初始化数据库
-(void)sharvalewLoadData{
//创建预编译指针
const char * sqla = "create table if not exists sql (id integer primary key, image text)";
//将sql语句编译到预编译指针内部
sqlite3_stmt * stmt;
//执行sql语句
sqlite3_prepare(sqliteDB, sqla, -1, &stmt, nil);
if (sqlite3_step(stmt) == SQLITE_DONE)
{
NSLog(@"创建成功");
}
//释放预编译指针
sqlite3_finalize(stmt);
}
//添加数据
-(void)addOvalewLoadData:(Model *)model1{
//创建预编译指针
const char * sqla = "insert into sql values(null,?)";
//将sql语句编译到预编译指针内部
sqlite3_stmt * stmt;
//执行sql语句
sqlite3_prepare(sqliteDB, sqla, -1, &stmt, nil);
sqlite3_bind_text(stmt, 1 , [model1.image UTF8String], -1, nil);
if (sqlite3_step(stmt) == SQLITE_DONE)
{
NSLog(@"添加成功");
}
//释放预编译指针
sqlite3_finalize(stmt);
}
//查询数据
-(NSData *)selevalewLoadData{
//查询
const char * sqla = "select * from sql";
//将sql语句编译到预编译指针内部
sqlite3_stmt * stmt;
//执行语句
sqlite3_prepare(sqliteDB, sqla, -1, &stmt, nil);
while (sqlite3_step(stmt) == SQLITE_ROW)
{
//初始化model
Model * model = [[Model alloc]init];
//将model字符串化
model.image = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
//data接受model中image的值
data = [[NSData alloc]initWithBase64Encoding:model.image];
}
//返回值
return [NSData dataWithData:data];
}
//关闭数据
-(void)colervalewLoadData{
sqlite3_close(sqliteDB);
}
@end
//在ViewController.m
导入头文件#import "QRCodeGenerator.h"
#import "Model.h"
#import "LoadData.h"
定义全部变量
@interface ViewController (){
Model *mod;
ZBarReaderViewController *reader;
BDRecognizerViewController *bdv;
//参数设置 key 秘钥
BDRecognizerViewParamsObject *bdf;
NSMutableData *allData;
}
定义一个将要的事件
-(void)viewWillAppear:(BOOL)animated
{
//打开数据库
[[LoadData sharLoadData]openvalewLoadData];
//查询
NSData * data = [[LoadData sharLoadData ]selevalewLoadData];
//添加
self.img1.image = [UIImage imageWithData:data];
//关闭
[[LoadData sharLoadData]colervalewLoadData];
}
- (IBAction)swewm:(id)sender {
self.img.image = [QRCodeGenerator qrImageForString:self.textIF.text imageSize:self.view.frame.size.width];
}
- (IBAction)baocun:(id)sender {
//打开数据库
[[LoadData sharLoadData]openvalewLoadData];
//初始化model
mod = [[Model alloc]init];
//nsdata接受img的值
NSData * data = UIImageJPEGRepresentation(self.img.image, 0.5f);
mod.image = [data base64Encoding];
//添加数据
[[LoadData sharLoadData]addOvalewLoadData:mod];
//关闭数据库
[[LoadData sharLoadData]colervalewLoadData];
[self select];
}
-(void)select{
//打开数据库
[[LoadData sharLoadData]openvalewLoadData];
//转换查询
NSData *data = [[LoadData sharLoadData]selevalewLoadData];
//添加
self.img1.image = [UIImage imageWithData:data];
//关闭
[[LoadData sharLoadData]colervalewLoadData];
}