#pragma mark -- 数库

- (void)createDatabase{

//路径

NSString *path = [NSString stringWithFormat:@"%@/Documents/maxueshan.db",NSHomeDirectory()];

//创建

_database = [[FMDatabase alloc]initWithPath:path];

//打开

[_database open];

NSLog(@"%@",path);

}

//重写析构方法   ,  不能写 [super dealloc]

- (void)dealloc{

//关闭数据库

[_database close];

}

#pragma mark -- 创建表格

- (void)createTable{

NSString *sql = @"create table if not exists Student(stuName text,stuAge text, stuSex text)";

BOOL isSuccess = [_database executeUpdate:sql];

if (isSuccess) {

NSLog(@"table表格创建成功");

}else {

NSLog(@"table表格创建失败");

}

}

//增

- (void)insertDataWithArray:(NSArray *)array{

if (array.count == 0) {

//数组为空,没有模型

return;

}

//1.SQL语句  拼接

NSString *sql = @"insert into Student values(?,?,?)";

//2.遍历数组

for (StudentModel *model in array) {

//

[_database executeUpdate:sql,model.stuName,model.stuAge,model.stuSex];

}

}

//删

- (void)deleteDataWithArray:(NSArray *)array{

if (array.count == 0) {

return ;

}

NSString *sql = @"delete from Student where stuName=? and stuAge=? and stuSex=?";

for (StudentModel *model in array) {

[_database executeUpdate:sql,model.stuName,model.stuAge,model.stuSex];

}

}

//改

- (BOOL)updateDataWithSourceModle:(StudentModel *)sourceModel andNewModel:(StudentModel *)newModel{

if (sourceModel && newModel) {

//两个模型都不为空时,才执行 数据的修改

NSString *sql = @"update Student set stuName=? , set stuAge=? , set stuSex=? where stuName=? and stuAge=? and stuSex=?";

BOOL isSuccess = [_database executeUpdate:sql,newModel.stuName,newModel.stuAge,newModel.stuSex,sourceModel.stuName,sourceModel.stuAge,sourceModel.stuSex];

return isSuccess;

}

return NO;

}

//查

- (NSArray *)selectAllDatas{

//1.数组,接收模型

NSMutableArray *dataArr = [NSMutableArray array];

//2.sql 语句

NSString *sql = @"select *from Student";

//3.查询

FMResultSet *set = [_database executeQuery:sql];

//4.遍历,分装模型

while ([set next]) {

NSString *name = [set stringForColumn:@"stuName"];

NSString *age  = [set stringForColumn:@"stuAge"];

NSString *sex  = [set stringForColumn:@"stuSex"];

NSDictionary *dic = @{@"stuName":name,@"stuAge":age,@"stuSex":sex};

StudentModel *model = [StudentModel createModelWithDic:dic];

[dataArr addObject:model];

}

return dataArr;

}

//姓名

- (NSArray *)selectForName:(NSString *)name {

if (name.length == 0) {

return nil;

}

NSMutableArray *dataArr = [NSMutableArray array];

NSString *sql = @"select * form Student where stuName=? ";

FMResultSet *set = [_database executeQuery:sql,name];

while ([set next]) {

NSString *name = [set stringForColumn:@"stuName"];

NSString *age = [set stringForColumn:@"stuAge"];

NSString *sex = [set stringForColumn:@"stuSex"];

NSDictionary *dic = @{@"stuName":name,@"stuAge":age,@"stuSex":sex};

StudentModel *model = [StudentModel createModelWithDic:dic];

[dataArr addObject:model];

}

return dataArr;

}

FMDB----SQL----数据库的更多相关文章

  1. iOS——使用FMDB进行数据库操作(转载)

    iOS 使用FMDB进行数据库操作 https://github.com/ccgus/fmdb [摘要]本文介绍iOS 使用FMDB进行数据库操作,并提供详细的示例代码供参考. FMDB 使用方法 A ...

  2. KTV项目 SQL数据库的应用 结合C#应用窗体

    五道口北大青鸟校区 KTV项目 指导老师:袁玉明 歌曲播放原理 SQL数据库关系图 C#解决方案类图 第一步:创建数据库连接方法和打开方法和关闭方法! public class DBHelper { ...

  3. jquery autocomplete实现读取sql数据库自动补全TextBox

    转自我本良人 原文 jquery autocomplete实现读取sql数据库自动补全TextBox 项目需要这样子一个功能,其他部门提的意见,只好去实现了哦,搞了好久才弄出来,分享一下. 1.前台页 ...

  4. SQL数据库

    SQL是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言.在使用它时,只需要发出“做什么”的命令,“怎么做” ...

  5. 基于Qt5.5.0的sql数据库、SDK_tts文本语音朗读的CET四六级单词背诵系统软件的编写V1.0

    作者:小波 QQ:463431476 请关注我的博客园:http://www.cnblogs.com/xiaobo-Linux/ 我的第二款软件:CET四六级单词背诵软件.基于QT5.5.0.sql数 ...

  6. matlab连接sql数据库

    最近项目还涉及到matlab连接数据库,下面我就记录如何进行配置使得matlab能够连接sql数据库.由于最近工程做的多一些,所以分享的都在工程配置上,当初为了这些配置可是反复卸载与重装,算法其实也有 ...

  7. SQL SERVER 2008配置Database Mail –用SQL 数据库发邮件

    SQL SERVER 2008配置Database Mail –用SQL  数据库发邮件 https://blogs.msdn.microsoft.com/apgcdsd/2011/06/28/sql ...

  8. Eclipse连接到My sql数据库之前操作

    Eclipse连接到My sql数据库之前操作 1:首先是安装My sql数据库(为了减少你的麻烦,按照下面的连接,下载即可)百度云链接:http://pan.baidu.com/s/1mitWmbm ...

  9. Eclipse连接到My sql数据库的操作总结/配置数据库驱动

    Eclipse连接到MYSQL数据库的操作 (自己亲测,开始学习Eclipse(我的Eclipse版本是4.5.2,Jdbc驱动器的jar包版本是5.1.7,亲测可以使用)连接到数据库的时候,发现网上 ...

  10. 【C#】SQL数据库助手类2.0(自用)

    using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...

随机推荐

  1. [Chapter 3 Process]Practice 3.2 Including the initial parent process, how many processes are created by the program shown in Figure?

    3.2 Including the initial parent process, how many processes are created by the program shown in Fig ...

  2. RandomForestClassifier(随机森林检测每个特征的重要性及每个样例属于哪个类的概率)

    #In the next recipe, we'll look at how to tune the random forest classifier. #Let's start by importi ...

  3. onclick事件没有反应的五种可能情况

    转自:https://blog.csdn.net/qujing_1120/article/details/76853039 onclick=”alert()” 事件没有反应的几种情况.第一:<i ...

  4. App.CSharp.Grid的ICells接口

    using System;using System.Collections.Generic;using System.Text;using System.Drawing;using System.Wi ...

  5. Xcode快捷

  6. 第4章 ZK基本特性与基于Linux的ZK客户端命令行学习 4-1 zookeeper常用命令行操作

    ls path [watch] watch是一个监督者.quota是zookeeper的子目录.目录就是节点的意思,对于zookeeper来说它是以一个节点来说的,所以说/就是根节点,zookeepe ...

  7. ZROI2018提高day3t2

    传送门 分析 我们设A[i]表示点i有几个矿,B[i]表示这之中有几个矿是第一次出现,所以点i的贡献即为 (2^B[i]-1)*(2^(A[i]-B[i])) 注意减一的原因是第一次出现的矿应至少有一 ...

  8. 100851K King’s Inspection

    传送门 题目大意 给你一张图,求这张图的汉密尔顿回路. 分析 因为m≤n+20,所以如果存在回路一定是在一个环中加入了至多20条边.我们先考虑dfs,但我们发现如果出现图1这种情况就会是复杂度爆炸 图 ...

  9. Luogu 4388 付公主的矩形

    还是挺妙的. 发现对于一个$r$行$c$列的矩阵,穿过的格子数$n = r + c - gcd(r, c)$,题目中其实给定了这个$n$,要我们计算满足这个式子的$r$和$c$的个数. 发现$n$一定 ...

  10. 第二篇:MySQL库相关操作

    一 系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等performance_schema: MyS ...