iOS——使用FMDB进行数据库操作(转载)
iOS 使用FMDB进行数据库操作
[摘要]本文介绍iOS 使用FMDB进行数据库操作,并提供详细的示例代码供参考。
FMDB 使用方法
ARC 和 MRC
项目中使用 ARC 还是 MRC,对使用 FMDB 都没有任何影响,FMDB 会在编译项目时自动匹配。
使用
在 FMDB 中有三个重要的类:
FMDatabase
:是一个提供 SQLite 数据库的类,用于执行 SQL 语句。FMResultSet
:用在FMDatabase
中执行查询的结果的类。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 | } |
iOS——使用FMDB进行数据库操作(转载)的更多相关文章
- iOS- Swift:使用FMDB进行数据库操作(线程安全:增删改查)
1.前言 GitHub上2000多颗星的FMDB数据库框架想来大家都很熟悉, 今天用Swift对其进行了一个完成的数据存储读流程 写完之后用博客分享之,与大家一起交流, 希望对需要的朋友提供些帮助 ...
- 【iOS】FMDB/SQLCipher数据库加解密,迁移
2016-04-19更新:本文代码可能有些问题,请移步 http://zhengbomo.github.io/2016-04-18/sqlcipher-start/ 查看 sqlite应用几乎在所有的 ...
- iOS下FMDB的多线程操作(二)
上一篇记录不使用FMDatabaseQueue来使用多线程,这一篇记录一下使用FMDatabaseQueue的方式. 需要注意的时queue操作中不能嵌套queue操作,否则会各种错误. 当使用FMD ...
- iOS下FMDB的多线程操作(一)
iOS中一些时间比较长的操作都应该放在子线程中,以避免UI的卡顿.而sqlite 是非线程安全的,故在多线程中不能共用同一个数据库连接,否则会导致EXC_BAD_ACCESS.所以我们可以在子线程中创 ...
- ios开发FMDB导入SQLCipher加密数据库
转:http://www.2cto.com/kf/201407/315727.html [iOS]FMDB/SQLCipher数据库加解密,迁移
- iOS 数据库操作(使用FMDB)
iOS 数据库操作(使用FMDB) iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.Plausibl ...
- QF——iOS中的数据库操作:SQLite数据库,第三方封装库FMDB,CoreData
SQLite数据库: SQLite是轻量级的数据库,适合应用在移动设备和小型设备上,它的优点是轻量,可移植性强.但它的缺点是它的API是用C写的,不是面向对象的.整体来说,操作起来比较麻烦.所以,一般 ...
- iOS学习笔记(十六)——数据库操作(使用FMDB)
iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepers ...
- iOS数据库操作(使用FMDB)
iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepers ...
随机推荐
- 前端知识体系:JavaScript基础-作用域和闭包-this的原理以及几种使用场景
一.问题由来: var obj = { foo: function () { console.log(this.bar) }, bar: 1 }; var foo = obj.foo; var bar ...
- Selenium常用API的使用java语言之10-获取断言信息
不管是在做功能测试还是自动化测试,最后一步需要拿实际结果与预期进行比较.这个比较的称之为断言. 我们通常可以通过获取title .URL和text等信息进行断言.text方法在前面已经讲过,它用于获取 ...
- stm32 HardFault_Handler调试及问题查找方法——飞思卡尔
看到有朋友遇到Hard Fault 异常错误,特地找到一篇飞思卡尔工程师写的一片经验帖,定位Hard Fault 异常. Kinetis MCU 采用 Cortex-M4 的内核,该内核的 Fault ...
- 54、servlet3.0-ServletContainerInitializer
54.servlet3.0-ServletContainerInitializer Shared libraries(共享库) / runtimes pluggability(运行时插件能力) 1.S ...
- 论文笔记-Deep Affinity Network for Multiple Object Tracking
作者: ShijieSun, Naveed Akhtar, HuanShengSong, Ajmal Mian, Mubarak Shah 来源: arXiv:1810.11780v1 项目:http ...
- sql server 常见约束
1.not null 非空约束 ①强制列不接受空值 ②例:创建表时,name varchar(6) not null, 2.unique 唯一性约束 ①约束唯一标识数据库表中的每条记录 ②unique ...
- 洛谷 P3374 【模板】树状数组 1 & P3368 【模板】树状数组 2 题解
一维树状数组的作用主要是单点修改,单点查询,区间修改,区间查询. 模板1是单点修改,区间查询:模板2是单点查询,区间修改. 模板1: #include<iostream> #include ...
- 四十.创建Redis集群 管理集群
环境准备 准备 6台(51-56) redis服务器 以默认配置运行redis服务即可 一.创建Redis集群 1.启用集群功能( 51-56 都要配置) ]# netstat -antupl ...
- 后台(一)vue+element-ui(全局配置)
vue init webpack 项目名称 npm install axios //先安装! npm install --save axios vue-ax ...
- LSTM-航班人数预测
小书匠深度学习LSTM 郑重声明,文章大部分翻译自: Time Series Prediction with LSTM Recurrent Neural Networks in Python with ...