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 ...
随机推荐
- [Dart] splitMapJoin
var str3 = '''Multi Line String'''; print( str3.splitMapJoin( RegExp(r'^', multiLine: true), // Matc ...
- vue computed、filters 用法笔记
computed 在使用时的效果是属性,不是函数. 其次,computed 的值是“智能”响应的,依赖必须都是这个实例自己的东西,如果某个计算需要依赖传入的值,则推荐使用 methods. filte ...
- 005_simulink建立条件子系统
1. 条件执行子系统 a) 使能子系统:是控制信号大于零时执行的子系统.在控制信号穿越零点由负变正的时步点上,使能子系统开始执行.只要子系统的控制信号保持正值,使能子系统就会保持在执行的状态 b) ...
- hive,把一个表中计算好的数据,存到另一个外部表中
直接上代码: 第一部分: case class OrdPacsresult_obj(pk_dcpv: String, result_obj: String) 第二部分: def ordsubj: Un ...
- 批量给文件去BOM(百度网盘)
链接:https://pan.baidu.com/s/1jC8RkyC0xX1lA-zZjOyDsw 提取码:geko 第一步:浏览你要移除BOM编码的文件夹.第二步:点击移除bom,随后会弹出提示框 ...
- Nginx设置禁止通过IP访问服务器并且只能通过指定域名访问
为了避免别人把未备案的域名解析到自己的服务器IP而导致服务器被断网,需要在nginx上设置禁止通过IP访问服务器,只能通过域名访问. 最关键的一点是,在server的设置里面添加这么一行: liste ...
- ES 的基本用法
ES的基本用法 ES的基本概念 1> 集群和节点 一个es集群是由一个或多和es节点组成的集合 每一个集群都有一个名字, 如之前的wali 每个节点都有自己的名字, 如之前的master, sl ...
- simcom7600ce-t LBS function
Welcome to minicom 2.7 OPTIONS: I18n Compiled on Nov 15 2018, 20:20:38.Port /dev/ttyUSB2, 00:55:23 P ...
- pandas记录
pandas的map方法使用 import pandas as pd import numpy as np data = pd.DataFrame(np.random.randint(0, 10, ( ...
- CF293E Close Vertice
如果没有边数限制就是裸的淀粉质,如果有了加上一个树状数组记边数就行了. #include<stdio.h> #include<stdlib.h> #include<str ...