突然找到自己去年写的demo写个文章纪念一下,直接上代码 不想说那么多 ,干脆.
使用的是DBHelper 的代码,然后我自己封装了一下
如何使用部分
1.建表
//建表
SqlModel * sql = [[SqlModel alloc] initAndSqliteName:@"EMBABD"];
if (![isFirst isEqualToString:@"1"]) {
[defaults setObject:@"1" forKey:@"isFirst"];
if ([sql createTable:@"queryUserAllPerm" andType:@"arr BLOB"]) {
NSLog(@"创建表成功");
}else{
NSLog(@"创建表失败");
}
}
2.添加
//添加方法
SqlModel * sql = [[SqlModel alloc] initAndSqliteName:@"EMBABD"];
if ([sql addObjectInTable:dic[@"content"] andTableName:@"queryUserAllPerm"]) {
NSLog(@"添加数据成功");
}else{
NSLog(@"添加数据失败");
}
3.查询
//查询方法
NSArray * arr = [sql findObjectInTable:@"queryUserAllPerm" andType:@"arr"];
4.删除
//删除方法
if ([sql delectTable:@"queryUserAllPerm" andWhere:nil]) {
NSLog(@"删除表成功");
}else{
NSLog(@"删除表失败");
}
.h页面代码
//数据库操作
@interface SqlModel : NSObject
@property (readonly, nonatomic, retain)DBHelper * helper;
//init方法创建数据库 name:数据库的名字 只需要建一次
- (instancetype)initAndSqliteName:(NSString *)name;
//创建表返回是否成功。 tableName:建立的表名 type:对应数据类型,不填写为查询全部(需要用sql语句输入)
- (BOOL)createTable:(NSString *)tableName andType:(NSString *)type;
//查找表里的对象 tableName:查找的表名 type:查找的条件(需要用sql语句输入)
- (NSArray *)findObjectInTable:(NSString *)tableName andType:(NSString *)type;
//添加表内对象 objet:添加的内容id类型的 tableName:添加的表名
- (BOOL)addObjectInTable:(id)object andTableName:(NSString *)tableName;
//删除表 tableName:删除表的内容 where:删除条件 如果不写为默认全删除(需要用sql语句输入)
- (BOOL)delectTable:(NSString *)tableName andWhere:(NSString *)where;//删除
@end
.m页面代码
@implementation SqlModel
- (instancetype)initAndSqliteName:(NSString *)name{
if(self = [super init]){
NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *createPath = [NSString stringWithFormat:@"%@/%@.sqlite",pathDocuments,name];
NSLog(@"%@",createPath);
_helper = [[DBHelper alloc] initDatabaseWithDBPath:createPath];
}
return self;
}
- (BOOL)createTable:(NSString *)tableName andType:(NSString *)type{
NSString * sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (%@);",tableName,type];
return [self.helper createTable:sql];
}
- (NSArray *)findObjectInTable:(NSString *)tableName andType:(NSString *)type{
NSString * sql = [NSString stringWithFormat:@"select * from %@",tableName];//sqlite语句
NSArray * arr = [self.helper findAllForSql:sql andValues:nil withResultSetBlock:^id(FMResultSet *rs){
NSData * messageId = [rs dataForColumn:type];
NSArray *imageArr = [NSKeyedUnarchiver unarchiveObjectWithData:messageId];
return imageArr;
}];
return arr;
}
- (BOOL)addObjectInTable:(id)object andTableName:(NSString *)tableName{
NSString * sql = [NSString stringWithFormat:@"insert into %@ values (?)",tableName];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object];
NSArray * args = [NSArray arrayWithObjects:data,nil];
return [self.helper executeUpdateForSql:sql andValues:args];
}
- (BOOL)delectTable:(NSString *)tableName andWhere:(NSString *)where{
where = where?[NSString stringWithFormat:@"where %@",where]:@"";
NSString * sql = [NSString stringWithFormat:@"delete from %@ %@",tableName,where];
return [self.helper executeUpdateForSql:sql andValues:nil];
}
@end
需要的人可以直接copy走吧!记得帮我点点❤ 爱你哟!
网友评论