FMDB的简单用法
使用cocoaPods将FMDB下载到工程
第一步:引入框架,引入支持类库(libsqlite3.0.tbd)
#import <FMDB.h>

声明属性
@interface ViewController () /// 声明数据库对象 @property (nonatomic, strong) FMDatabase *dataBase; /// 声明存储路径 @property (nonatomic, strong) NSString *filePath; @end
#pragma mark - 创建表
- (void)viewDidLoad {
[super viewDidLoad];
// 创建表
[self createTable];
}
#pragma mark - 创建表
- (void)createTable
{
// 第一步:创建sql语句
NSString *createSql = @"create table if not exists t_student(id integer primary key autoincrement not null, name text not null, age integer not null, sex text not null)";
// 第二步:找到存储路径
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:];
self.filePath = [document stringByAppendingPathComponent:@"student.sqlite"];
NSLog(@"%@", self.filePath);
// 第三步:使用路径初始化FMDB对象
self.dataBase = [FMDatabase databaseWithPath:self.filePath];
// 第四步:数据库执行相关的操作
// 需要判断数据库打开的时候才进行执行语句
if ([self.dataBase open]) {
BOOL result = [self.dataBase executeUpdate:createSql];
if (result) {
NSLog(@"建表成功");
} else {
NSLog(@"建表失败");
}
}
// 第五步:关闭数据库
[self.dataBase close];
}
#pragma mark - 添加学生
- (IBAction)insertIntoActon:(id)sender {
// 打开数据库
[self.dataBase open];
// 第二步:进行相关操作
NSArray *nameArray = [NSArray arrayWithObjects:@"MBBoy", @"炸天", @"小明", nil];
; i < nameArray.count; i ++) {
NSString *name = [nameArray objectAtIndex:i];
// 插入语句
NSString *insertSql = @"insert into t_student(name, age, sex) values (?, ?, ?)";
BOOL result = [self.dataBase executeUpdate:insertSql, name, @, @"男"];
if (result) {
NSLog(@"插入成功");
} else {
NSLog(@"插入失败");
NSLog(@"%d", result);
}
}
// 第五步:关闭数据库
[self.dataBase close];
}
#pragma mark - 更改学生
- (IBAction)upDateAction:(id)sender {
// 打开数据库
[self.dataBase open];
BOOL result = [self.dataBase executeUpdate:@"update t_student set name = ? where name = ?", @"孟玲旭", @"MBBoy"];
if (result) {
NSLog(@"更改成功");
} else {
NSLog(@"更改失败");
}
[self.dataBase close];
}
#pragma mark - 删除学生
- (IBAction)deleteAction:(id)sender {
// 打开数据库
[self.dataBase open];
BOOL result = [self.dataBase executeUpdate:@"delete from t_student where name = ?", @"孟玲旭"];
if (result) {
NSLog(@"删除成功");
} else {
NSLog(@"删除失败");
}
}
#pragma mark - 查询学生
- (IBAction)searchAction:(id)sender {
// 打开数据库
[self.dataBase open];
// 查询结果使用的类FMResultSet
FMResultSet *resultSet = [self.dataBase executeQuery:@"select * from t_student"];
// 遍历除需要的所有内容
while ([resultSet next]) {
NSString *name = [resultSet objectForColumnName:@"name"];
NSInteger age = [resultSet intForColumn:@"age"];
NSString *sex = [resultSet objectForColumnName:@"sex"];
NSLog(@"name = %@, age = %ld, sex = %@", name, age, sex);
}
[self.dataBase close];
}
#pragma mark - 插入很多学生
- (IBAction)insertManyStudent:(id)sender {
// 已队列的形式添加数据是FMDB比较常用的添加方式
// FMDB不支持多个线程同时操作, 所以一般以串行的实现方式实现相关操作。
// 打开数据库
[self.dataBase open];
// 第一步:创建操作对类
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:self.filePath];
// 标识:记录是否成功
__block BOOL isSuccess = YES;
// 第二步:把所需要的事件打包放在操作队列中
[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
// 串行队列
isSuccess = [db executeUpdate:, @"男"] && isSuccess;
isSuccess = [db executeUpdate:, @"女"] && isSuccess;
isSuccess = [db executeUpdate:, @"女"] && isSuccess;
// 如果有错误
if (!isSuccess) {
// block返回的参数rollback进行处理(bool 类型的指针)
*rollback = YES;
return ;
}
}];
[self.dataBase close];
}
FMDB的简单用法的更多相关文章
- CATransition(os开发之画面切换) 的简单用法
CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...
- jquery.validate.js 表单验证简单用法
引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...
- NSCharacterSet 简单用法
NSCharacterSet 简单用法 NSCharacterSet其实是许多字符或者数字或者符号的组合,在网络处理的时候会用到 NSMutableCharacterSet *base = [NSMu ...
- [转]Valgrind简单用法
[转]Valgrind简单用法 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian S ...
- Oracle的substr函数简单用法
substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H' *从字符串第一个字符开始截取长度为1的字符串 subst ...
- Ext.Net学习笔记19:Ext.Net FormPanel 简单用法
Ext.Net学习笔记19:Ext.Net FormPanel 简单用法 FormPanel是一个常用的控件,Ext.Net中的FormPanel控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ...
- TransactionScope简单用法
记录TransactionScope简单用法,示例如下: void Test() { using (TransactionScope scope = new TransactionScope()) { ...
- WPF之Treeview控件简单用法
TreeView:表示显示在树结构中分层数据具有项目可展开和折叠的控件 TreeView 的内容是可以包含丰富内容的 TreeViewItem 控件,如 Button 和 Image 控件.TreeV ...
- listActivity和ExpandableListActivity的简单用法
http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html 今天自己简单的总结了listActivity和Expandable ...
随机推荐
- 6种炫酷的CSS3按钮边框动画特效
6种炫酷的CSS3按钮边框动画特效Button border animate 用鼠标滑过下面的按钮看看效果! Draw Draw Meet Center Spin Spin Circle Spin T ...
- tableView等滚动视图滚动时收缩上下导航栏与标签栏
代码如下,今天有点忙,不想细说了,看不明白可以联系我 // // LQXViewController.m // LQXCallBackBar // // Created by 刘祺旭 on 15/4/ ...
- chart.js在html中画曲线图
http://www.bootcss.com/p/chart.js/docs/ http://www.chartjs.org/docs/ 中有详细讲解 一.简介 Chart.js是一个基于HTML ...
- Extjs4中的常用组件:Grid、Tree和Form
至此我们已经学习了Data包和布局等API.下面我们来学习作为Extjs框架中我们用得最多的用来展现数据的Grid.Tree和Form吧! 目录: 5.1. Grid panel 5.1.1. Col ...
- Warning: Attempt to present on whose view is not in模态跳转问题
错误分析: controller A present controller B ,前提是A的view要存在,如果不存在,就会报这个错. 解决方法: 将 ...
- IAR for STM8 错误
一个IAR for STM8 v1.3 的工程,换到1.4版后出现如下错误 unable to allocate space for sections/blocks with a total esti ...
- VB.NET中网络编程所需组件WinHTTP的添加
VB.NET中网络编程所需组件: WinHTTP组件:项目-->添加引用-->COM选项卡-->Microsoft WinHTTP Services,version 5.1--> ...
- 微信小程序之----底部菜单action-sheet
action-sheet action-sheet是从底部弹出的选择菜单,子菜单通过action-sheet-item和action-sheet-cancel指定,action-sheet-item是 ...
- 2017-01-11小程序form表单提交
小程序form表单提交 1.小程序相对于之前的WEB+PHP建站来说,个人理解为只是将web放到了微信端,用小程序固定的格式前前端进行布局.事件触发和数据的输送和读取,服务器端可以用任何后端语言写,但 ...
- leetcode day6
[13]Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...