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 ...
随机推荐
- 如何在C中定义多行宏定义?
请参阅下面的示例,其中我将交换两个变量的值. do-while(0)结构很不错 #include <stdio.h> #define swap(x,y,T) do { \ T temp = ...
- PHP读取文件内容的方法
下面我们就为大家详细介绍PHP读取文件内容的两种方法. 第一种方法:fread函数 <?php $file=fopen('1.txt','rb+'); echo fread($file,file ...
- Ftp客户端需要TSL功能的文件上传
Ftp客户端需要TSL功能 1.由于最近做了一个项目,需要把打包的文件传输到对方的FTP服务器上,但是用普通的java连接ftp客户端总是连接不上去,对方却说ftp客户端需要开通TSL功能. 直接上代 ...
- 073_使用 shell 脚本打印如下图形
#!/bin/bash #打印第一组图片#for(())为类 C 语言的语法格式,也可以使用 for i in;do;done 的格式替换#for((i=1;i<=9;i++))循环会执行 9 ...
- 使用 Java 创建聊天客户端-1
1.聊天客户端文本框的搭建. 项目截图:java project 代码: (1).ChatManager.java package com.nantian.javachatclient.main; i ...
- 容器网络启用RDMA高速通讯-Freeflow
容器网络启用RDMA高速通讯-Freeflow 容器网络启用RDMA高速通讯-Freeflow 本文编译自: Freeflow,https://github.com/openthings/Freefl ...
- docker部署vue前端
1.下载安装nginx image docker pull nginx:latest 2.准备将编译后的代码上传到主机上 3.编写dockerfile, nginx conf,并创建镜像 Docker ...
- Pytest从测试类外为测试用例动态注入数据
今天Nelly问我Pytest能不能支持从TestClass类外传入参数?从类外批量传入各个test方法需要的参数.因为数据文件可能有很多情况,不方便依次匹配. 然而又必须用类对用例进行归类及复用,数 ...
- Allure自动化测试报告之修改allure测试报告logo
1.安装allure 2.进入 /usr/local/Cellar/allure/2.10.0/libexec/config 3.在allure.yml添加 - custom-logo-plugin ...
- Mac 解决终端:-bash: /Users/xxx/.profile: No such file or directory
touch ~/.profile加入export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin 参考:https://www.zhihu.com/ ...