FMDB基本使用
2016-04-11 08:13
302 查看
#import "PersonDBTool.h" #import <FMDB.h> @implementation PersonDBTool static FMDatabase *_dataBase; + (void)initialize{ //1.创建数据,并且打开/连接到沙盒中的数据库 NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *filePath = [documentPath stringByAppendingPathComponent:@"person.db"]; _dataBase = [FMDatabase databaseWithPath:filePath]; //2.打开 BOOL result = [_dataBase open]; if(result){ //3.创建表 BOOL createTableResult = [_dataBase executeUpdate:@"create table if not exists t_person(id integer primary key,name text,age integer);"]; if(createTableResult){ NSLog(@"create Table ok!!!"); }else{ NSLog(@"error!!!"); } } } #pragma mark - FMDB DML语句 + (void)insertPerson:(Person *)person{ [_dataBase executeUpdateWithFormat:@"insert into t_person(name,age) values(%@,%d) ;",person.name,person.age]; } + (void)updatePerson:(Person *)person{ [_dataBase executeUpdateWithFormat:@"update t_person set age = %d where name = %@",person.age,person.name]; } + (void)deletePersonWithId:(int)personId{ [_dataBase executeUpdateWithFormat:@"delete from t_person where id = %d",personId]; } #pragma mark - DQL语句 + (NSArray *)queryAllPerson{ NSMutableArray *persons = [NSMutableArray array]; FMResultSet *resultSet = [_dataBase executeQuery:@"select * from t_person"]; while ([resultSet next]) {//遍历到每一条记录 NSString *pname = [resultSet stringForColumn:@"name"]; int age = [resultSet intForColumn:@"age"]; Person *p = [[Person alloc] init]; p.name = pname; p.age = age; [persons addObject:p]; } return persons.copy; } + (NSArray *)queryPersonWithKeyWord:(NSString *)keyWord{ NSMutableArray *persons = [NSMutableArray array]; FMResultSet *resultSet = [_dataBase executeQuery:[NSString stringWithFormat:@"select * from t_person where name like '%%%@%%';",keyWord]]; while ([resultSet next]) {//遍历到每一条记录 NSString *pname = [resultSet stringForColumn:@"name"]; int age = [resultSet intForColumn:@"age"]; Person *p = [[Person alloc] init]; p.name = pname; p.age = age; [persons addObject:p]; } return persons.copy; } @end
相关文章推荐
- C语言文件遍历
- 贪心总结
- Activiti5 开发介绍1
- iOS学习笔记16-数据库SQLite
- 超详细cordova环境配置(windows)及实例
- 无法打开模块文件,对象引用被隐藏
- jsp动态导航栏
- ViewPagerTransformer实现3d轮播图
- 支付宝钱包系统架构内部
- Apache服务无法启动的解决方法
- 翻转单词顺序列
- [置顶] angularJS 常用指令小结
- 使用SurfaceView和MediaPlayer实现视频做为背景
- 【1-1】使用pyhon连接mysq 数据库查询成功
- C语言时间转换
- 作业链接
- 为什么浮点数不能直接与零值比较?谢谢
- 理解LSTM
- 笔记-系统源码常用的Content Provider
- SQL Server安全(10/11):行级别安全(Row-Level Security)