FMDB的使用
来源:互联网 发布:国产蓝牙耳机知乎 编辑:程序博客网 时间:2024/05/21 10:16
FMDB 是对 SQLite API 轻量级二次封装。所以整体使用思路还是类似 SQLite API 的思路。由于 FMDB 是基于 SQLite API 的,所以使用 FMDB 也需要导入 SQLite 的动态库文件。
FMDB 中除了“查询”以外,任何操作都归属于“更新”行为。
下面我们通过一个demo来一起看一下,FMDB操作sqlite的增删改查,我们需要建立一个页面,页面中添加4个BUTTON,通过button的bug值来区分click事件对应的功能,首先在viewdidload中我们应该先生成数据库文件。也就是初始化数据库
- (void)viewDidLoad { [super viewDidLoad]; FMDatabase *db = [FMDatabase databaseWithPath:@"/Users/tarena/Desktop/test.db"]; self.db = db; if ([db open]) { NSLog(@"打开成功!"); if ([db executeUpdate:@"create table if not exists names (name text)"]) { NSLog(@"创建表成功!"); }else NSLog(@"创建表失败"); }}
以下分别为增删改查功能:
case 0://增加数据 { NSString *sql = [NSString stringWithFormat:@"insert into names values ('%@')",self.myTF.text]; if ([self.db executeUpdate:sql]) { NSLog(@"插入数据成功!"); }else NSLog(@"插入数据失败"); } break;
case 1://删除数据 { NSString *sql = [NSString stringWithFormat:@"delete from names where name='%@'",self.myTF.text]; if ([self.db executeUpdate:sql]) { NSLog(@"删除数据成功!"); }else NSLog(@"删除数据失败!"); } break;
case 2://修改数据,此处为修改所有姓名 { NSString *sql = [NSString stringWithFormat:@"update names set name='%@'",self.myTF.text]; if ([self.db executeUpdate:sql]) { NSLog(@"修改数据成功!"); }else NSLog(@"修改数据失败!"); } break;
查询数据的时候需要使用一个新的类,FMResultSet
case 3://查询数据 { FMResultSet *result = [self.db executeQuery:@"select * from names"]; while ([result next]) { NSString *name = [result stringForColumn:@"name"]; //如果查询年龄需要使用Intfor.... //int age = [result intForColumn:@"age"]; NSLog(@"%@",name); } } break;
FMDB的操作需要嵌套sqlite的基本操作,如果有看不明白的地方请参考sqlite基础
http://blog.csdn.net/lee727n/article/details/71171120
0 0
- FMDB的使用
- FMDB库的使用
- FMDB的使用
- FMDB的使用
- FMDB的使用
- FMDB的使用
- FMDB库的使用
- sqlite3 FMDB的使用
- FMDB的使用
- iOS FMDB的使用
- FMDB的基本使用
- FMDB的使用
- FMDB的使用
- FMDB的使用
- FMDB的简单使用
- FMDB的使用
- FMDB的简单使用
- FMDB的使用
- 汇编指令的学习2——常用的ARM指令
- 基于深度学习的目标检测(object detection)—— rcnn、fast-rcnn、faster-rcnn
- ubuntu安装Sublime Text 3(并安装anaconda插件)
- JDK 8 新特性学习(二) Lambda表达式、集合流之操作
- RTSP协议、RTMP协议、HTTP协议的区别
- FMDB的使用
- 如何通过抓包抓出rtmp的完整留地址
- 系统对象的方法
- ConcurrentQueue的内存泄露BUG
- poj 2421 Constructing Roads kruskal克鲁斯卡尔最小生成树
- Error: Android SDK not found. Make sure that it is installed. If it is not at the default location,
- (hdu1007)Quoit Design,求最近点对
- React native webView Ios端禁止手势放大
- 【数据结构】看图理解单链表的反转