#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. list array解析(总算清楚一点了)

    # -*- coding: utf-8 -*- """ Created on Tue Aug 09 23:04:51 2016 @author: Administrato ...

  2. LAMP 2.6 Apache 禁止指定user_agent

    user_agent 我把它叫做浏览器标识, 目前主流的浏览器有 IE. chrome. Firefox. 360. iphone上的 Safari.Android 手机上的.百度搜索引擎.googl ...

  3. 【知识碎片】SQL篇

    43.group by多个字段 查询每个班级男女生各多少人 Select count(id),xingbie,banji from tablename group by xingbie,banji 4 ...

  4. ActiveMQ (二) JMS入门

    JMS入门 前提:安装好了ActiveMQ  ActiveMQ安装 Demo结构: 首先pom.xml引入依赖: <dependency> <groupId>org.apach ...

  5. Struts2 结合 Freemarker 实例

     Freemarker 是一个不依赖 web 容器的模板引擎,一个基于模板生成文本输出的工具.其工作的原理如下图: freemarker 不是一个 web 应用的框架,而适合作为 web 应用的一个组 ...

  6. solr :term 查询, phrase查询, boolean 查询

    搜索总体有:term 查询, phrase查询, boolean 查询 1. SOLR搜索覆盖度和准确度保证的三个搜索方式: 保证准确率: AND: Search for two different ...

  7. go 语言介绍

    特点1. 静态类型,编译开源语言 2. 脚本化的语法,支持多种编程范式(函数式,面向对象) 3. 原生,给力的并发支持并发编程 Go语言的优势: 1.脚本化的语法:开发效率高,容易上手 2.静态类型+ ...

  8. 基于IFC的大型三维城市群体——智慧城市模拟

  9. spoj1716 Can you answer these queries III

    传送门 (分析见正睿2018.10.1笔记) 代码 #include<iostream> #include<cstdio> #include<cstring> #i ...

  10. ZROI2018提高day3t3

    传送门 分析 我们对于每一个可以匹配的字符都将其从栈中弹出,然后他的哈希值就是现在栈中的字符哈希一下.然后我们便可以求出对于哪些位置它们的哈希值是一样的,即它们的状态是一致的.而这些点可以求出它们的贡 ...