标签:
#import "DBManager.h"
#import "FMDatabase.h"
@implementation DBManager
{
FMDatabase *_fmdb;
NSLock *_lock;
}
+(DBManager *)shareManager{
static DBManager *manager=nil;
static dispatch_once_t once;
dispatch_once(&once, ^{
if (manager==nil) {
manager=[[DBManager alloc]init];
}
});
return manager;
}
-(id)init{
self=[super init];
if (self) {
_lock=[[NSLock alloc]init];
// [_lock lock]; 加锁
// [_lock unlock]; 解锁
NSString *path=[NSHomeDirectory() stringByAppendingString:@"/Documents/user.db"];
_fmdb=[[FMDatabase alloc]initWithPath:path];
if ([_fmdb open]) {
NSString *createSql=@"create table if not exists userInfo(id integer primary key autoincrement,name varchar(256),age integer,image blob)";//创建一个userInfo表,表中有id、name、age、image 四个属性。 blob 是用来存储2进制的。 integer 整型varchar(256) 字符型
// create table if not exists 表名(属性 属性的类型,属性 属性的类型)
//primary key 主键 autoincrement 自增
BOOL success=[_fmdb executeUpdate:createSql];
if (!success) {
NSLog(@"create:%@",_fmdb.lastError);
}
}
}
return self;
}
// 插入数据
-(void)insertModel:(UserModel *)model{
[_lock lock];
NSString *insertSql=@"insert into userInfo(name,age,image) values(?,?,?)";
//insert into 表名(属性名,属性名) values(?,?)
UIImage *image=model.headImage;
NSData *data=UIImagePNGRepresentation(image);//UIImagePNGRepresentation 这个方法是可以将png类型的图片转化成 NSData
BOOL success=[_fmdb executeUpdate:insertSql,model.name,model.age,data];//executeUpdate 后面需要去接 继承与NSObject类所实例化的对象 否则会抛出EXC_BAD_ACCESS错误
if (!success) {
NSLog(@"%@",_fmdb.lastError);
}
[_lock unlock];
}
//删除数据
-(void)deleteDataWith:(NSNumber *)dataID{
[_lock lock];
NSString *deleteSql=@"delete from userInfo where id=?";// delete from 表名 where 属性=?
BOOL success=[_fmdb executeUpdate:deleteSql,dataID];
if (!success) {
NSLog(@"%@",_fmdb.lastError);
}
[_lock unlock];
}
-(void)updateWith:(UserModel *)model{
[_lock lock];
NSString *updateSql=@"update userInfo set name=?,age=?,image=? where id =?";// update 表名 set 属性名=?,属性名=? where 属性名=?
UIImage *image=model.headImage;
NSData *data=UIImagePNGRepresentation(image);
NSNumber *IDNum=[NSNumber numberWithInteger:[model.id1 integerValue]];
BOOL success=[_fmdb executeUpdate:updateSql,model.name,model.age,data,IDNum];
if (!success) {
NSLog(@"%@",_fmdb.lastError);
}
[_lock unlock];
}
-(NSArray *)selectAllData{
[_lock lock];
NSString *selSQL=@"select * from userInfo";//* 查找全部 select * from 表名
FMResultSet *set=[_fmdb executeQuery:selSQL];
NSMutableArray *array=[[NSMutableArray alloc]init];
while ([set next]) {
UserModel *model=[[UserModel alloc]init];
model.name=[set stringForColumn:@"name"];
NSData *data=[set dataForColumn:@"image"];
UIImage *image=[UIImage imageWithData:data];//把NSData类型的数据转化成image
model.headImage=image;
model.age=[NSNumber numberWithInteger:[[set stringForColumn:@"age"] integerValue]];
[array addObject:model];
}
[_lock unlock];
return [array copy];
}
// 查找一条语句是否存在
-(BOOL)isOrNotIN:(NSNumber *)idOne{
NSString *sql=@"select * from userInfo where id=?";//
FMResultSet *set=[_fmdb executeQuery:sql];
return [set next];
}
@end
标签:
原文地址:http://www.cnblogs.com/liaods/p/4787591.html