iOS 使用FMDB进行数据库操作
iOS 使用FMDB进行数据库操作
[摘要]本文介绍iOS 使用FMDB进行数据库操作,并提供详细的示例代码供参考。
1、首先要先导入第三方类库FMdatabase。
2、获得存放数据库文件的沙盒地址。
1 | +(NSString *)databaseFilePath |
2 | { |
3 | |
4 | NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
5 | NSString *documentPath = [filePath objectAtIndex:0]; |
6 | NSLog(@"%@",filePath); |
7 | NSString *dbFilePath = [documentPath stringByAppendingPathComponent:@"db.sqlite"]; |
8 | return dbFilePath; |
9 | |
10 | } |
3、创建数据库的操作
1 | +(void)creatDatabase |
2 | { |
3 | db = [[FMDatabasedatabaseWithPath:[selfdatabaseFilePath]] retain]; |
4 | } |
4、创建表
1 | +(void)creatTable |
2 | { |
3 | //先判断数据库是否存在,如果不存在,创建数据库 |
4 | if (!db) { |
5 | [selfcreatDatabase]; |
6 | } |
7 | //判断数据库是否已经打开,如果没有打开,提示失败 |
8 | if (![db open]) { |
9 | NSLog(@"数据库打开失败"); |
10 | return; |
11 | } |
12 | |
13 | //为数据库设置缓存,提高查询效率 |
14 | [dbsetShouldCacheStatements:YES]; |
15 | |
16 | //判断数据库中是否已经存在这个表,如果不存在则创建该表 |
17 | if(![dbtableExists:@"people"]) |
18 | { |
19 | [db executeUpdate:@"CREATE TABLES people(people_id INTEGER PRIMARY KEY AUTOINCREAMENT, name TEXT, age INTEGER) "]; |
20 | |
21 | |
22 | NSLog(@"创建完成"); |
23 | } |
24 | |
25 | } |
5、增加表数据
1 | +(void)insertPeople:(People *)aPeople |
2 | { |
3 | if (!db) { |
4 | [selfcreatDatabase]; |
5 | } |
6 | |
7 | if (![db open]) { |
8 | NSLog(@"数据库打开失败"); |
9 | return; |
10 | } |
11 | |
12 | [dbsetShouldCacheStatements:YES]; |
13 | |
14 | if(![dbtableExists:@"people"]) |
15 | { |
16 | [selfcreatTable]; |
17 | } |
18 | //以上操作与创建表是做的判断逻辑相同 |
19 | //现在表中查询有没有相同的元素,如果有,做修改操作 |
20 | FMResultSet *rs = [dbexecuteQuery:@"select * from people where people_id = ?",[NSStringstringWithFormat:@"%d",aPeople.peopleID]]; |
21 | if([rs next]) |
22 | { |
23 | NSLog(@"dddddslsdkien"); |
24 | [dbexecuteUpdate:@"update people set name = ?, age = ? where people_id = 1",aPeople.name,[NSStringstringWithFormat:@"%d",aPeople.age]]; |
25 | } |
26 | //向数据库中插入一条数据 |
27 | else{ |
28 | [dbexecuteUpdate:@"INSERT INTO people (name, age) VALUES (?,?)",aPeople.name,[NSStringstringWithFormat:@"%d",aPeople.age]]; |
29 | } |
30 | |
31 | } |
6、删除数据
1 | +(void)deletePeopleByID:(int)ID |
2 | { |
3 | if (!db) { |
4 | [selfcreatDatabase]; |
5 | } |
6 | |
7 | if (![db open]) { |
8 | NSLog(@"数据库打开失败"); |
9 | return; |
10 | } |
11 | |
12 | [dbsetShouldCacheStatements:YES]; |
13 | |
14 | //判断表中是否有指定的数据, 如果没有则无删除的必要,直接return |
15 | if(![dbtableExists:@"people"]) |
16 | { |
17 | return; |
18 | } |
19 | //删除操作 |
20 | [db executeUpdate:@"delete from people where people_id = ?", [NSStringstringWithFormat:@"%d",ID]]; |
21 | |
22 | [db close]; |
23 | } |
7、修改操作与增加操作的步骤一致
8、查询
1 | +(NSArray *)getAllPeople |
2 | { |
3 | |
4 | if (!db) { |
5 | [selfcreatDatabase]; |
6 | } |
7 | |
8 | if (![db open]) { |
9 | NSLog(@"数据库打开失败"); |
10 | return nil; |
11 | } |
12 | |
13 | [dbsetShouldCacheStatements:YES]; |
14 | |
15 | if(![dbtableExists:@"people"]) |
16 | { |
17 | return nil; |
18 | } |
19 | |
20 | //定义一个可变数组,用来存放查询的结果,返回给调用者 |
21 | NSMutableArray *peopleArray = [[NSMutableArrayalloc] initWithArray:0]; |
22 | //定义一个结果集,存放查询的数据 |
23 | FMResultSet *rs = [dbexecuteQuery:@"select * from people"]; |
24 | //判断结果集中是否有数据,如果有则取出数据 |
25 | while ([rs next]) { |
26 | People *aPeople = [[People alloc] init]; |
27 | |
28 | aPeople.peopleID = [rs intForColumn:@"people_id"]; |
29 | aPeople.name = [rs stringForColumn:@"name"]; |
30 | aPeople.age = [rs intForColumn:@"age"]; |
31 | //将查询到的数据放入数组中。 |
32 | [peopleArray addObject:aPeople]; |
33 | } |
34 | return [peopleArray autorelease]; |
35 | } |
iOS 使用FMDB进行数据库操作相关推荐
- 使用FMDB进行数据库操作
FMDB: https://github.com/ccgus/fmdb 按照教程可以通过pods的方式进行依赖引入. 简单的增删改查操作,代码如下: #import "ViewControl ...
- iOS数据库操作之FMDB
SQLite一种轻量级关系数据库,在嵌入式系统中使用比较广泛. 在iOS中使用SQLite需要添加库libsqlite3.0.dylib,并引入头文件#import <sqlite3.h> ...
- iOS 数据库操作(使用FMDB)
iOS 数据库操作(使用FMDB) iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.Plausibl ...
- 数据库操作(使用FMDB)
iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepers ...
- FMDB - 数据库操作
iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepers ...
- iOS学习笔记(十五)——数据库操作(SQLite)
SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的 ...
- IOS数据库操作SQLite3使用详解(转)
iPhone中支持通过sqlite3来访问iPhone本地的数据库. 具体使用方法如下 1:添加开发包libsqlite3.0.dylib 首先是设置项目文件,在项目中添加iPhone版的sqlite ...
- IOS数据库操作SQLite3使用详解
sqlite数据库iosdatabasesqlinteger 目录(?)[+] iPhone中支持通过sqlite3来访问iPhone本地的数据库. 具体使用方法如下 1:添加开发包libsqlite ...
- iOS SQLite3数据库操作
iOS中数据持久化分为四种:属性列表.对象归档.SQLite3和Core Data,SQLite3数据库操作是一个必不或缺的技术. SQLite3简介 SQLite3数据库是移动端(iOS.Andro ...
- 【iOS】—— FMDB的基础用法
一.基本介绍: 1.简介: 由于iOS中原生的 SQLite API 使用时非常不方便 ,于是就出现了一系列将 SQLite API 进行封装的库,例如FMDB.PlausibleDatabase.s ...
最新文章
- 2018 CVPR GAN 相关论文调研 (自己分了下类,附地址哦)
- AppCan用cookie实现记住密码功能 [APP]
- idea前进和后退快捷键_必备技能:IDEA一定要懂的32条快捷键
- linux里根目录下的/bin,usr里的/usr/bin还有local里的/usr/local/bin有什么区别?
- mysql主从数据库验证_数据库主从一致性验证
- Python利用turtle绘制五角星
- 2006年9月全国计算机等级考试三级数据库技术笔试试卷(含有答案)
- 微软以75亿美元收购GitHub
- ubuntu中vim编辑器的安装与基本使用
- Quartus II 13.1与Modelsim SE的安装
- 久期方程 matlab,有限差分法解薛定谔方程与MATLAB实现
- 计算机网络:随机访问介质访问控制之ALOHA协议
- 使用Docker部署ONLYOFFICE Document Server
- 网易云音乐歌单制作词云图
- javascript复习资料第一部分
- 解决电脑连接蓝牙耳机默认音量过大的问题
- 那些英语不好的人现在在职场都混得如何?
- 瓴羊 Quick BI 提供移动端自助分析整体解决方案,Fine BI、Smart BI 何时赶上?
- CCF CSP 序列查询新解
- SQL触发器实例讲解(本文是来自百度文库)
热门文章
- [转]VS2008 集成openCV过程
- [转]OPENGL中GLU和GLUT工具箱
- 获取ACCESS_TOKEN接口
- XMPP作为一个工具的意义
- UVA 10651 - Pebble Solitaire
- httprequest存储的是字符内容 而文本内容是以字节形式上传的;所以普通的取值方式无法从httprequest取到值...
- HashMap在并发场景下踩过的坑
- pat 团体天梯 L3-011. 直捣黄龙
- 面向对象的四个基本特征
- -bash: wget 未找到命令的解决办法