数据库类库FMDB

发表于:2014-5-13 10:36

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:激情为梦想而生    来源:51Testing软件测试网采编

  前言
  SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库。iOS SDK很早就支持了SQLite,在使用时,只需要加入 libsqlite3.dylib 依赖以及引入 sqlite3.h 头文件即可。但是,原生的SQLite API在使用上相当不友好,在使用时,非常不便。于是,开源社区中就出现了一系列将SQLite API进行封装的库,而FMDB (https://github.com/tryingx/fmdb-master) 则是开源社区中的优秀者。
  第一步 引入:sqlite3的类库
  第二步 引入:FMDB的类库
  主要包括以下几方面
  引入文件结束以后就可以使用了,使用很简单
  第一步 建立一个数据库类
1 NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // 获取document文件的路径
2 /**
3  *  强调一点就是对于数据库的名:我们可以用"" 或用 NULL
4  *
5  *  An empty string (@""). An empty database is created at a temporary location. This database is deleted with the FMDatabase connection is closed.
6  也就是说如果我们使用(@""),会创建一个临时数据库,当我们数据库关闭后会自动删除
7  NULL. An in-memory database is created. This database will be destroyed with the FMDatabase connection is closed.
8  在内存中给你创建一个数据库
9  */
10 NSString *dbPath = [docPath stringByAppendingPathComponent:@"student.sqlite"];
11 NSLog(@"%@", dbPath); // 拼接字符串
12 FMDatabase *dataBase = [FMDatabase databaseWithPath:dbPath];
13 [dataBase open];
14 // 用来判断数据库打开是否成功
15 if (![dataBase open]) {
16     NSLog(@"error");
17 }
  第二步 就是执行一些常用的操作
  常用操作:
  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 );
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号