FMDB的简单使用
2016-05-06 13:39 甘雨路 阅读(354) 评论(0) 编辑 收藏 举报#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> /** * 源代码的链接地址 * 链接: http://pan.baidu.com/s/1pKTxfyb 密码: eqa3 * */ @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. return YES; } @end
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end
#import "ViewController.h" #import "UIImage+CreatImageWithColor.h" #import "LFPerson.h" #import "LFFMDBOperation.h" @interface ViewController () @property(nonatomic, strong) LFFMDBOperation *operation; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSArray *titles = @[@"增",@"删",@"改",@"查"]; //按钮的宽 float buttonW = 50; //按钮的高 float buttonH = 40; //间隙 float gap = 20; //初始位置 float originX = ([UIScreen mainScreen].bounds.size.width - (gap+buttonW)*titles.count)/2.0; //创建按钮 for (int i = 0; i < titles.count; i++) { // 初始化button UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; //设置尺寸 button.frame = CGRectMake(originX+(gap+buttonW)*i, 100, buttonW, buttonH); //添加标签号 button.tag = i; //设置背景颜色 [button setBackgroundColor:[UIColor orangeColor]]; //设置标题 [button setTitle:titles[i] forState:UIControlStateNormal]; //设置字体的颜色 [button setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal]; //设置选中时的背景图片 [button setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:246/255.0 green:246/255.0 blue:246/255.0 alpha:0.45]] forState:UIControlStateHighlighted]; //添加监听事件 [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; //添加到控制器的视图上 [self.view addSubview:button]; } //初始化LFFMDBOperation self.operation = [[LFFMDBOperation alloc] init]; //创表 [self.operation creatTableWithString:@"CREATE TABLE IF NOT EXISTS PERSON (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age text NOT NULL, address text NOT NULL);" fileName:@"Person.sqlite"]; } /** * 按钮事件 */ - (void)buttonAction:(UIButton *)sender{ // NSLog(@"%ld",sender.tag); switch (sender.tag) { case 0: { LFPerson *person = [[LFPerson alloc] init]; person.name = @"GYL"; person.age = 25; person.address = @"广东"; [self insertData:person]; } break; case 1: { LFPerson *person = [[LFPerson alloc] init]; person.identifier = 1; person.name = @"GYL——"; person.age = 18; person.address = @"湖北"; [self deleteDataWithPerson:person]; //删除所有的数据 // [self deleteAllData]; } break; case 2: { LFPerson *person = [[LFPerson alloc] init]; person.identifier = 1; person.name = @"GYL——"; person.age = 18; person.address = @"湖北"; [self updateData:person]; } break; case 3: [self queryData]; break; default: break; } } /** * 插入数据 * * @param person 插入的对象 */ - (void)insertData:(LFPerson *)person{ //插入数据 [self.operation dealDataWithString:@"INSERT INTO PERSON (name,age,address) VALUES (%@,%@,%@)" andPerson:person]; } /** * 删除数据库 */ - (void)deleteAllData{ [self.operation deleteAllDataWithString:@"DROP TABLE IF EXISTS PERSON;"]; //创表(删除表后,若不重新创建,则无法进行其他操作了,因为表不存在了) [self.operation creatTableWithString:@"CREATE TABLE IF NOT EXISTS PERSON (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age text NOT NULL, address text NOT NULL);" fileName:@"Person.sqlite"]; } /** * 删除某一条数据 * * @param person 被删除的对象 */ - (void)deleteDataWithPerson:(LFPerson *)person{ [self.operation deleteOneDataWithString:@"DELETE FROM PERSON WHERE id = %@" andPerson:person]; } /** * 更新数据 * * @param person 更新的对象 */ - (void)updateData:(LFPerson *)person{ [self.operation replaceDataWithString:@"UPDATE PERSON SET name = ?,age = ?, address = ? WHERE id = ?" andPerson:person]; } // 将数据转成模型数据 - (NSArray *)changeToModelWithArray:(NSArray *)dataArr{ NSMutableArray *arr = [NSMutableArray array]; for (int i = 0; i < dataArr.count; i++) { NSArray *array = [dataArr objectAtIndex:i]; // 转成对象 LFPerson *person = [[LFPerson alloc] init]; person.identifier = [array[0] intValue]; person.name = array[1]; person.age = [array[2] integerValue]; person.address = array[3]; [arr addObject:person]; } return arr; } /** * 查询数据 */ - (void)queryData{ //将对象的属性名存入数组 NSArray *arr = @[@"name",@"age",@"address"]; //从数据库中进行查询操作 NSArray * array = [self.operation queryWithString:@"SELECT * FROM PERSON" paramaArray:arr]; // NSLog(@"%@",array); // 转成对象 NSArray * resultArr = [self changeToModelWithArray:array]; for (LFPerson *person in resultArr) { NSLog(@"%ld-%@-%ld-%@",(long)person.identifier,person.name,(long)person.age,person.address); } } @end
#import <UIKit/UIKit.h> @interface UIImage (CreatImageWithColor) /** * 通过颜色生成该纯颜色的图片 * * @param color 生成图片的颜色 * * @return 返回图片 */ + (UIImage *)imageWithColor:(UIColor *)color; /** * 通过颜色生成该纯颜色的图片 * * @param color 生成图片的颜色 * @param width 生成图片的宽 * @param height 生成图片的高 * * @return 返回图片 */ + (UIImage *)imageWithColor:(UIColor *)color imageWidth:(CGFloat)width imageWithHeight:(CGFloat)height; @end
#import "UIImage+CreatImageWithColor.h" @implementation UIImage (CreatImageWithColor) + (UIImage *)imageWithColor:(UIColor *)color{ return [UIImage imageWithColor:color imageWidth:100 imageWithHeight:100]; } + (UIImage *)imageWithColor:(UIColor *)color imageWidth:(CGFloat)width imageWithHeight:(CGFloat)height{ //开启基于位图的图形上下文 UIGraphicsBeginImageContextWithOptions(CGSizeMake(width,height), NO, 0.0); // 设置画笔的颜色 [color set]; // 画矩形,并填充 UIRectFill(CGRectMake(0, 0, width, height)); //获取图片 UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); //关闭上下文 UIGraphicsEndImageContext(); return resultImage; } @end
#import <Foundation/Foundation.h> @interface LFPerson : NSObject @property (nonatomic, assign) int identifier;// 对应主键号 @property (nonatomic, copy) NSString *name;//名字 @property (nonatomic, assign) NSInteger age;//年龄 @property (nonatomic, copy) NSString *address;//地址 @end
#import "LFPerson.h" @implementation LFPerson @end
#import <Foundation/Foundation.h> @class LFPerson; @interface LFFMDBOperation : NSObject /** * 创建表 * * @param SQLString SQL的命令语句 * @param fileName 数据库的文件名 * * @return 返回BOOL值 */ - (BOOL)creatTableWithString:(NSString *)SQLString fileName:(NSString*)fileName; /** * 增加数据 * * @param SQLString SQL的命令语句 * @param params 相应的字段 */ - (void)dealDataWithString:(NSString *)SQLString andPerson:(LFPerson *)person; /** * 删除所有数据 * * @param SQLString SQL的删除命令语句 */ - (void)deleteAllDataWithString:(NSString *)SQLString; /** * 删除某个对象 * * @param SQLString SQL的删除命令语句 * @param person 删除的对象 */ - (void)deleteOneDataWithString:(NSString *)SQLString andPerson:(LFPerson *)person; /** * 更新数据 * * @param SQLString SQL的更新命令语句 * @param person 更新的对象(更新的内容不包括主键) */ - (void)replaceDataWithString:(NSString *)SQLString andPerson:(LFPerson *)person; /** * 查询 * * @param SQLString SQL的命令语句 * @param params 相应的字段 * * @return 返回查询结果 */ - (NSArray *)queryWithString:(NSString *)SQLString paramaArray:(NSArray *)params; @end
#import "LFFMDBOperation.h" #import "FMDB.h" #import "LFPerson.h" @interface LFFMDBOperation () @property(nonatomic, strong) FMDatabase *db; @end @implementation LFFMDBOperation - (BOOL)creatTableWithString:(NSString *)SQLString fileName:(NSString*)fileName{ //获取数据库文件的路径 NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *dbFileName = [doc stringByAppendingPathComponent:fileName]; //获取数据库 FMDatabase *db = [FMDatabase databaseWithPath:dbFileName]; BOOL result = NO; //打开数据库 if ([db open]) { result = [db executeUpdate:SQLString]; if (result) { NSLog(@"成功创表"); }else{ NSLog(@"创表失败"); } } self.db = db; return result; } - (void)dealDataWithString:(NSString *)SQLString andPerson:(LFPerson *)person{ [self.db executeUpdateWithFormat:SQLString,person.name,[NSString stringWithFormat:@"%ld",person.age],person.address]; } - (void)deleteAllDataWithString:(NSString *)SQLString{ [self.db executeUpdate:SQLString]; } - (void)deleteOneDataWithString:(NSString *)SQLString andPerson:(LFPerson *)person{ [self.db executeUpdateWithFormat:SQLString,@(person.identifier)]; } - (void)replaceDataWithString:(NSString *)SQLString andPerson:(LFPerson *)person{ [self.db executeUpdate:SQLString,person.name,@(person.age),person.address,@(person.identifier)]; } - (NSArray *)queryWithString:(NSString *)SQLString paramaArray:(NSArray *)params{ //初始化数组(存放所有的查询结果) NSMutableArray *resultArr = [NSMutableArray array]; //执行查询语句 FMResultSet *resutlSet = [self.db executeQuery:SQLString]; //遍历结果 while ([resutlSet next]) { // 初始化数组,存放一个对象的所有字段 NSMutableArray *arr = [NSMutableArray array]; //主键 int ID = [resutlSet intForColumn:@"id"]; //建主键存入arr [arr addObject:[NSNumber numberWithInt:ID]]; for (int i = 0; i < params.count; i++) { // 取相应字段的数据存入arr NSString *str = [resutlSet stringForColumn:params[i]]; // 将对象的每个字段添加到arr数组 [arr addObject:str]; } // 将每一个对象数组arr(对象数组arr存放着对象所有的字段,以及主键)存入resultArr数组 [resultArr addObject:arr]; } return resultArr; } @end