第三方数据库类库使用(FMDB)
来源:互联网 发布:如何安装车载导航软件 编辑:程序博客网 时间:2024/05/16 04:45
下载地址:https://github.com/ccgus/fmdb
前言
SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库。iOS SDK很早就支持了SQLite,在使用时,只需要加入 libsqlite3.dylib 依赖以及引入 sqlite3.h 头文件即可。但是,原生的SQLite API在使用上相当不友好,在使用时,非常不便。于是,开源社区中就出现了一系列将SQLite API进行封装的库,而FMDB (https://github.com/ccgus/fmdb) 则是开源社区中的优秀者废话不多说直接将使用
第一步 引入:sqlite3的类库
第二步 引入:FMDB的类库
主要包括以下几方面
引入文件结束以后就可以使用了,使用很简单
第一步 建立一个数据库类
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // 获取document文件的路径 /** * 强调一点就是对于数据库的名:我们可以用"" 或用 NULL * * An empty string (@""). An empty database is created at a temporary location. This database is deleted with the FMDatabase connection is closed. 也就是说如果我们使用(@""),会创建一个临时数据库,当我们数据库关闭后会自动删除 NULL. An in-memory database is created. This database will be destroyed with the FMDatabase connection is closed. 在内存中给你创建一个数据库 */ NSString *dbPath = [docPath stringByAppendingPathComponent:@"student.sqlite"]; NSLog(@"%@", dbPath); // 拼接字符串 FMDatabase *dataBase = [FMDatabase databaseWithPath:dbPath]; [dataBase open]; // 用来判断数据库打开是否成功 if (![dataBase open]) { NSLog(@"error"); }第二步 就是执行一些常用的操作
常用操作:
1.创建一个表
NSString *sql = @"CREATE TABLE student(_id Integer primary key, name text, password text, email text)"; BOOL isCreateTable = [dataBase executeStatements:sql]; NSLog(@"%d", isCreateTable );
2 插入数据
NSString *sqlInsert = @"INSERT INTO student (name, password, email) values ('张飞', '1232', '3343243')"; BOOL isInsertOK = [dataBase executeStatements:sqlInsert]; NSLog(@"%d", isInsertOK );
3,批量的创建表和插入数据
NSString *sql1 = @"create table bulktest1 (id integer primary key autoincrement, x text);" "create table bulktest2 (id integer primary key autoincrement, y text);" "create table bulktest3 (id integer primary key autoincrement, z text);" "insert into bulktest1 (x) values ('XXX');" "insert into bulktest2 (y) values ('YYY');" "insert into bulktest3 (z) values ('ZZZ');"; [dataBase executeStatements:sql1];当然也可以通过数组的方式插入数据
NSArray *stuentInfo = [NSArray arrayWithObjects:@"崔哥",@"213",@"23223@qq.com", nil]; [dataBase executeUpdate:@"insert into student (name, password, email) values (?,?,?)" withArgumentsInArray:stuentInfo];
4,匹配的方式插入数据
NSString *sqlInsert = @"INSERT INTO student (name, password, email) values (?,?,?)"; BOOL flag = [dataBase executeUpdate:sqlInsert, @"张三", @"123", @"3424234@qq.com"]; NSLog(@"%d", flag);
5.1查询所有数据
FMResultSet *result = [dataBase executeQuery:@"SELECT * FROM student"]; [self showInfo:result]; [result close];
- (void)showInfo:(FMResultSet *)result{ while ([result next]) { int _id = [result intForColumnIndex:0]; NSLog(@"id : %d", _id); NSString *name = [result stringForColumnIndex:1]; NSLog(@"name : %@", name); NSString *password = [result stringForColumnIndex:2]; NSLog(@"password : %@", password); NSString *email = [result stringForColumnIndex:3]; NSLog(@"email : %@", email); }}5.2 查询指定信息
NSString *sqlInsert = @"SELECT * from student where name = %@"; FMResultSet *result = [dataBase executeQueryWithFormat:sqlInsert, @"张三"]; NSLog(@"%@", result); [self showInfo:result];
6,执行删除操作
NSString *sql = @"delete from student where name = ?"; [dataBase executeUpdate:sql,@"张三"];
执行更新操作的步骤和删除操作一样,只不过sql语句不同而已
重要注意事项是:1 使用完数据库后记得关闭
2 查询结果完场后result也得记得关闭
以上就是FDBM的一些常用操作,具体用法访问https://github.com/ccgus/fmdb
0 0
- 第三方数据库类库使用(FMDB)
- 数据库第三方FMDB使用
- 怎么使用FMDB 第三方数据库
- 66.第三方类FMDB(SQL管理数据库)
- 第三方库 FMDB的使用
- FMDB的第三方库的使用
- IOS第三方数据库--FMDB
- iOS第三方数据库FMDB的使用介绍
- 第三方框架FMDB使用
- IOS-20-数据库操作之(SQLite第三方封装库FMDB)的基础入门使用及demo源码
- ios FMDB第三方库操作SQLite数据库
- iOS 第三方库 - FMDB
- iOS第三方数据库FMDB使用方法
- iOS中 FMDB第三方SQLite数据库
- 数据库:好用的第三方FMDB
- ios数据库sqlite3第三方框架FMDB
- 数据库第三方框架FMDB详细讲解
- iOS开发之Sqlite 第三方库FMDB的使用
- Python<10>文件基础
- 重要经验五:block作为属性的注意事项
- 学习C/C++语言:用数组处理Fibonacci数列
- centos 6.2安装ar8152网卡驱动
- Win7 64位机上安装VisualSVN-Servers失败解决方案
- 第三方数据库类库使用(FMDB)
- ios开发 block 在ARC机制下的内存管理
- Struts2中的ModelDriven机制及其运用
- 一对夫妇的离婚账单
- Linux系统对IO端口和IO内存的管理
- iOS中 设置 uiview的背景图片 制定uiview的某个角为圆角
- 简单工厂模式
- QUERY_REWRITE_INTEGRITY
- Android实践--apk反编译