iOS 使用FMDB进行数据库操作

https://github.com/ccgus/fmdb

[摘要]本文介绍iOS 使用FMDB进行数据库操作,并提供详细的示例代码供参考。

FMDB 使用方法

ARC 和 MRC

项目中使用 ARC 还是 MRC,对使用 FMDB 都没有任何影响,FMDB 会在编译项目时自动匹配。

使用

在 FMDB 中有三个重要的类:

  1. FMDatabase:是一个提供 SQLite 数据库的类,用于执行 SQL 语句。
  2. FMResultSet:用在 FMDatabase 中执行查询的结果的类。
  3. FMDatabaseQueue:在多线程下查询和更新数据库用到的类。

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 }

最新文章

  1. 常用webservice接口
  2. 单链表List的C实现
  3. 程序开发心理学阅读笔记——第II篇
  4. Android之文字点击链接
  5. 应聘.net开发工程师常见的面试题(五)
  6. php函数应用场景
  7. php 数组 array_intersect_key() array_unique()移除重复
  8. Codeforces Round #258 (Div. 2/C)/Codeforces451C_Predict Outcome of the Game(枚举)
  9. ViewTreeObserver简介
  10. 简单 TCP/IP 服务功能
  11. sigsuspend()阻塞:异步信号SIGIO为什么会被截胡?
  12. Charles 抓包工具(新猿旺学习总结)
  13. 【Javascript系列】变量作用域
  14. mongo 的导入和导出
  15. 阿里java代码检测工具p3c
  16. 内存优化总结:ptmalloc、tcmalloc和jemalloc(转)
  17. Cleartext HTTP traffic to ... not permitted
  18. 微信小程序 自定义单选复选按钮组的实现(用于实现购物车产品列表功能)
  19. 字典(dictionary) 的基本操作
  20. Layouts

热门文章

  1. 几个牛X的js开发技巧
  2. 转,sql server update set from inner 批量修改的使用
  3. ES 的基本用法
  4. MongoDB存储数据
  5. 51nod 1434
  6. mutex,thread(c++11 windows linux三种方式)
  7. P2831 愤怒的小鸟——状压
  8. Git项目代码统计-Python3版gitstats
  9. codeforces319C
  10. java创建数组几种方式