FMDB----SQL----数据库
#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----数据库的更多相关文章
- iOS——使用FMDB进行数据库操作(转载)
iOS 使用FMDB进行数据库操作 https://github.com/ccgus/fmdb [摘要]本文介绍iOS 使用FMDB进行数据库操作,并提供详细的示例代码供参考. FMDB 使用方法 A ...
- KTV项目 SQL数据库的应用 结合C#应用窗体
五道口北大青鸟校区 KTV项目 指导老师:袁玉明 歌曲播放原理 SQL数据库关系图 C#解决方案类图 第一步:创建数据库连接方法和打开方法和关闭方法! public class DBHelper { ...
- jquery autocomplete实现读取sql数据库自动补全TextBox
转自我本良人 原文 jquery autocomplete实现读取sql数据库自动补全TextBox 项目需要这样子一个功能,其他部门提的意见,只好去实现了哦,搞了好久才弄出来,分享一下. 1.前台页 ...
- SQL数据库
SQL是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言.在使用它时,只需要发出“做什么”的命令,“怎么做” ...
- 基于Qt5.5.0的sql数据库、SDK_tts文本语音朗读的CET四六级单词背诵系统软件的编写V1.0
作者:小波 QQ:463431476 请关注我的博客园:http://www.cnblogs.com/xiaobo-Linux/ 我的第二款软件:CET四六级单词背诵软件.基于QT5.5.0.sql数 ...
- matlab连接sql数据库
最近项目还涉及到matlab连接数据库,下面我就记录如何进行配置使得matlab能够连接sql数据库.由于最近工程做的多一些,所以分享的都在工程配置上,当初为了这些配置可是反复卸载与重装,算法其实也有 ...
- SQL SERVER 2008配置Database Mail –用SQL 数据库发邮件
SQL SERVER 2008配置Database Mail –用SQL 数据库发邮件 https://blogs.msdn.microsoft.com/apgcdsd/2011/06/28/sql ...
- Eclipse连接到My sql数据库之前操作
Eclipse连接到My sql数据库之前操作 1:首先是安装My sql数据库(为了减少你的麻烦,按照下面的连接,下载即可)百度云链接:http://pan.baidu.com/s/1mitWmbm ...
- Eclipse连接到My sql数据库的操作总结/配置数据库驱动
Eclipse连接到MYSQL数据库的操作 (自己亲测,开始学习Eclipse(我的Eclipse版本是4.5.2,Jdbc驱动器的jar包版本是5.1.7,亲测可以使用)连接到数据库的时候,发现网上 ...
- 【C#】SQL数据库助手类2.0(自用)
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...
随机推荐
- leetcode665
这道题目,主要是判断相邻的两个值的大小,并按照要求的方式,将数组的数值都修正为符合要求的值. 然后通过一次的遍历,计算所累积的移动次数. bool checkPossibility(vector< ...
- LAMP 3.4 mysql常用操作-2
给用户授权 > grant all on discuz.* to 'user1'@'192.168.1.%' identified by 'wangshaojun'; 指定库,用户名user1 ...
- hadoop-2.7.3.tar.gz + spark-2.0.2-bin-hadoop2.7.tgz + zeppelin-0.6.2-incubating-bin-all.tgz(master、slave1和slave2)(博主推荐)(图文详解)
不多说,直接上干货! 我这里,采取的是ubuntu 16.04系统,当然大家也可以在CentOS6.5里,这些都是小事 CentOS 6.5的安装详解 hadoop-2.6.0.tar.gz + sp ...
- python爬虫--编码问题y
1)中文网站爬取下来的内容中文显示乱码 Python中文乱码是由于Python在解析网页时默认用Unicode去解析,而大多数网站是utf-8格式的,并且解析出来之后,python竟然再以Unicod ...
- oracle语法练习汇总
全是自己一个一个敲出来的啊 啊 啊 --(1)查询20号部门的所有员工信息. --(2)查询所有工种为CLERK的员工的工号.员工名和部门名. select e.empno,e.ename,d.dna ...
- POJ 1151 扫描线 线段树
题意:给定平面直角坐标系中的N个矩形,求它们的面积并. 题解:建立一个四元组(x,y1,y2,k).(假设y1<y2)用来储存每一条线,将每一条线按x坐标排序.记录所有的y坐标以后排序离散化.离 ...
- MyBatis01 MyBatis基础知识【搞清楚原理】
1 MyBatis是什么 mybatis是一个持久层的框架,它对jdbc做了封装:是apache下的顶级项目 mybatis让程序将主要精力放在sql上,通过mybatis提供的映射方式,自由灵活生成 ...
- Windows系统 为 Visual Studio软件 搭建 OpenCV2 开发环境
Windows系统 为 Visual Studio软件 搭建 OpenCV2 开发环境 我们的电脑系统:Windows 10 64位 Visual Studio 软件:Visual Studio 20 ...
- win10 requireAdministrator设置开机自启动无效的解决方案
开发了一个wpf程序,需要管理员权限,设置了requireAdministrator 同时需要开机自启动,所以添加了注册表: using (RegistryKey key = Registry.Cur ...
- 算法Sedgewick第四版-第1章基础-025-用队列实现unix下的Directory命令
package algorithms.util; /************************************************************************** ...