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 ...
随机推荐
- pygame游戏图像绘制及精灵用法
1精灵文件 plane_sprites.py import pygame class GameSprite(pygame.sprite.Sprite): """飞机大战游 ...
- 29、[源码]-AOP原理-AnnotationAwareAspectJAutoProxyCreatovi
29.[源码]-AOP原理-AnnotationAwareAspectJAutoProxyCreatovi
- LeetCode 269. Alien Dictionary
原题链接在这里:https://leetcode.com/problems/alien-dictionary/ 题目: There is a new alien language which uses ...
- SCSS 教程
https://www.jianshu.com/p/a99764ff3c41 https://www.sass.hk/guide/ 1. 使用变量; sass让人们受益的一个重要特性就是它为css引入 ...
- mongodb mongod.lock文件及oplog文件
在mongodb的启动时,在数据目录下,会生成一个mongod.lock文件.如果在正常退出时,会清除这个mongod.lock文件,若要是异常退出,在下次启动的时候,会禁止启动,从而保留一份干净的一 ...
- 068_不登陆虚拟机,修改虚拟机网卡 IP 地址
#!/bin/bash #该脚本使用 guestmount 工具,Centos7.2 中安装 libguestfs-tools-c 可以获得 guestmount 工具#脚本在不登陆虚拟机的情况下,修 ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- 第12章、乐活人生的ABCDE
目录 第12章.乐活人生的ABCDE 什么时候该乐观 让自己乐观的ABC 确认ABC 你的ABC记录 反驳和转移注意 转移注意 反驳 保持距离 学习与自己争辩 证据 其他可能性 暗示 用处 你的反驳记 ...
- [bzoj 3534][Sdoi2014] 重建
传送门 Description T国有N个城市,用若干双向道路连接.一对城市之间至多存在一条道路. 在一次洪水之后,一些道路受损无法通行.虽然已经有人开始调查道路的损毁情况,但直到现在几乎没有消息传 ...
- CF1174C Ehab and a Special Coloring Problem(数论)
做法 与\(x\)互质的数填不同的数,把有向关系表示出来,发现边数是不能承受的 反过来想,成倍数关系填相同的数,把这些数想象成一条链,而这条链开始的数一定是质数,\(\sum\limits_{prim ...