使用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的简单用法的更多相关文章

  1. CATransition(os开发之画面切换) 的简单用法

    CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...

  2. jquery.validate.js 表单验证简单用法

    引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...

  3. NSCharacterSet 简单用法

    NSCharacterSet 简单用法 NSCharacterSet其实是许多字符或者数字或者符号的组合,在网络处理的时候会用到 NSMutableCharacterSet *base = [NSMu ...

  4. [转]Valgrind简单用法

    [转]Valgrind简单用法 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian S ...

  5. Oracle的substr函数简单用法

    substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H'  *从字符串第一个字符开始截取长度为1的字符串 subst ...

  6. Ext.Net学习笔记19:Ext.Net FormPanel 简单用法

    Ext.Net学习笔记19:Ext.Net FormPanel 简单用法 FormPanel是一个常用的控件,Ext.Net中的FormPanel控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ...

  7. TransactionScope简单用法

    记录TransactionScope简单用法,示例如下: void Test() { using (TransactionScope scope = new TransactionScope()) { ...

  8. WPF之Treeview控件简单用法

    TreeView:表示显示在树结构中分层数据具有项目可展开和折叠的控件 TreeView 的内容是可以包含丰富内容的 TreeViewItem 控件,如 Button 和 Image 控件.TreeV ...

  9. listActivity和ExpandableListActivity的简单用法

    http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html 今天自己简单的总结了listActivity和Expandable ...

随机推荐

  1. [ERROR] InnoDB: Cannot allocate memory for the buffer pool

    :: mysqld_safe Starting mysqld daemon with databases from /data/mysqldb -- :: [Note] /usr/local/mysq ...

  2. 从url下载图片--java与python实现方式比较

    从url下载图片--java与python实现方式比较 博客分类: 技术笔记小点滴 javapython图片下载  一.java的实现方式 首先读取图片 //方式一:直接根据url读取图片 priva ...

  3. linux 驱动入门5

    慢慢的开始转驱动,目前比较有时间,一定要把驱动学会.哎.人生慢慢路,一回头.已经工作了八九年了.努力.在买套房.改退休了.学驱动.个人认为首先要熟悉驱动框架.慢慢来.心急吃不了热豆腐. 看网上都说的设 ...

  4. 【POJ2104】【HDU2665】K-th Number 主席树

    [POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...

  5. Cookie的格式及组成

    转自:http://blog.csdn.net/talking12391239/article/details/9665185 Cookie由变量名和值组成,类似JavaScript变量.其属性里既有 ...

  6. FZU 1056 扫雷游戏

    水题.统计一下周围有几个雷. #include<cstdio> #include<cstring> #include<cmath> #include<algo ...

  7. 微信小程序之----接口调用方式

    最近开发了一个微信小程序版的任务管理系统,在向Java后台发送接口时遇到了一些问题,在这里做一个简单的总结. 官方接口 官方给出的接口叫做wx.request,请求方式比较简单,下面是官网给出的请求实 ...

  8. XCode 7上传遇到ERROR ITMS-90535 Unexpected CFBundleExecutable Key. 的解决办法

    去第三方的info.plist文件中,比如TencentOpenApi_IOS_Bundle.bundel 的info.plist ,删除 executable file 这一行

  9. Java使用POI实现数据导出excel报表

    Java使用POI实现数据导出excel报表 在上篇文章中,我们简单介绍了java读取word,excel和pdf文档内容 ,但在实际开发中,我们用到最多的是把数据库中数据导出excel报表形式.不仅 ...

  10. X-006 FriendlyARM tiny4412 u-boot移植之Debug串口用起来

    <<<<<<<<<<<<<<<<<<<<<<<<< ...