iOS之SQLite使用详解
#pragma mark - 1.引入<sqlite3.h>头文件
//添加libsqlite3.0.tbd
#import <sqlite3.h>
static sqlite3 *db;//是指向数据库的指针,我们其他操作都是用这个指针来完成
#pragma mark - 2.打开数据库
- (void)openSqlite {
//判断数据库是否为空,如果不为空说明已经打开
if(db != nil) {
NSLog(@"数据库已经打开");
return;
}
//获取文件路径
NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *strPath = [str stringByAppendingPathComponent:@"my.sqlite"];
NSLog(@"%@",strPath);
//打开数据库
//如果数据库存在就打开,如果不存在就创建一个再打开
int result = sqlite3_open([strPath UTF8String], &db);
//判断
if (result == SQLITE_OK) {
NSLog(@"数据库打开成功");
} else {
NSLog(@"数据库打开失败");
}
}
#pragma mark - 3.增删改查
//创建表格
- (void)createTable {
//1.准备sqlite语句
NSString *sqlite = [NSString stringWithFormat:@"create table if not exists 'student' ('number' integer primary key autoincrement not null,'name' text,'sex' text,'age'integer)"];
//2.执行sqlite语句
char *error = NULL;//执行sqlite语句失败的时候,会把失败的原因存储到里面
int result = sqlite3_exec(db, [sqlite UTF8String], nil, nil, &error);
//3.sqlite语句是否执行成功
if (result == SQLITE_OK) {
NSLog(@"创建表成功");
} else {
NSLog(@"创建表失败");
}
}
//添加数据
- (void)addStudent:(student *)stu {
//1.准备sqlite语句
NSString *sqlite = [NSString stringWithFormat:@"insert into student(number,name,age,sex) values ('%ld','%@','%@','%ld')",stu.number,stu.name,stu.sex,stu.age];
//2.执行sqlite语句
char *error = NULL;//执行sqlite语句失败的时候,会把失败的原因存储到里面
int result = sqlite3_exec(db, [sqlite UTF8String], nil, nil, &error);
if (result == SQLITE_OK) {
NSLog(@"添加数据成功");
} else {
NSLog(@"添加数据失败");
}
}
//删除数据
- (void)delete:(student*)stu {
//1.准备sqlite语句
NSString *sqlite = [NSString stringWithFormat:@"delete from student where number = '%ld'",stu.number];
//2.执行sqlite语句
char *error = NULL;//执行sqlite语句失败的时候,会把失败的原因存储到里面
int result = sqlite3_exec(db, [sqlite UTF8String], nil, nil, &error);
if (result == SQLITE_OK) {
NSLog(@"删除数据成功");
} else {
NSLog(@"删除数据失败%s",error);
}
}
//修改数据
- (void)updataWithStu:(student *)stu {
//1.sqlite语句
NSString *sqlite = [NSString stringWithFormat:@"update student set name = '%@',sex = '%@',age = '%ld' where number = '%ld'",stu.name,stu.sex,stu.age,stu.number];
//2.执行sqlite语句
char *error = NULL;//执行sqlite语句失败的时候,会把失败的原因存储到里面
int result = sqlite3_exec(db, [sqlite UTF8String], nil, nil, &error);
if (result == SQLITE_OK) {
NSLog(@"修改数据成功");
} else {
NSLog(@"修改数据失败");
}
}
//查询所有数据
- (NSMutableArray*)selectWithStu {
NSMutableArray *array = [[NSMutableArray alloc] init];
//1.准备sqlite语句
NSString *sqlite = [NSString stringWithFormat:@"select * from student"];
//2.伴随指针
sqlite3_stmt *stmt = NULL;
//3.预执行sqlite语句
int result = sqlite3_prepare(db, sqlite.UTF8String, -1, &stmt, NULL);//第4个参数是一次性返回所有的参数,就用-1
if (result == SQLITE_OK) {
NSLog(@"查询成功");
//4.执行n次
while (sqlite3_step(stmt) == SQLITE_ROW) {
student *stu = [[student alloc] init];
//从伴随指针获取数据,第0列
stu.number = sqlite3_column_int(stmt, 0);
//从伴随指针获取数据,第1列
stu.name = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)] ;
//从伴随指针获取数据,第2列
stu.sex = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)] ;
//从伴随指针获取数据,第3列
stu.age = sqlite3_column_int(stmt, 3);
[array addObject:stu];
}
} else {
NSLog(@"查询失败");
}
//5.关闭伴随指针
sqlite3_finalize(stmt);
return array;
}
#pragma mark - 4.关闭数据库
- (void)closeSqlite {
int result = sqlite3_close(db);
if (result == SQLITE_OK) {
NSLog(@"数据库关闭成功");
} else {
NSLog(@"数据库关闭失败");
}
}
iOS之SQLite使用详解的更多相关文章
- iOS 单元测试之XCTest详解(一)
iOS 单元测试之XCTest详解(一) http://blog.csdn.net/hello_hwc/article/details/46671053 原创blog,转载请注明出处 blog.csd ...
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...
- IOS—UITextFiled控件详解
IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...
- [转]iOS学习之UINavigationController详解与使用(三)ToolBar
转载地址:http://blog.csdn.net/totogo2010/article/details/7682641 iOS学习之UINavigationController详解与使用(二)页面切 ...
- IOS 友盟使用详解
IOS 友盟使用详解 这篇博客将会详细介绍友盟的使用,希望对博友们有所帮助. 首先我们在浏览器上搜索友盟. 在这里我们选择官网这个,进去友盟官网后我们按照下图进行选择. 接下来选择如下图 Next 这 ...
- iOS原生地图开发详解
在上一篇博客中:http://my.oschina.net/u/2340880/blog/414760.对iOS中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...
- [Android新手区] SQLite 操作详解--SQL语法
该文章完全摘自转自:北大青鸟[Android新手区] SQLite 操作详解--SQL语法 :http://home.bdqn.cn/thread-49363-1-1.html SQLite库可以解 ...
- [转]iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
转载地址:http://blog.csdn.net/totogo2010/article/details/7682433 iOS学习之UINavigationController详解与使用(一)添加U ...
- iOS中—触摸事件详解及使用
iOS中--触摸事件详解及使用 (一)初识 要想学好触摸事件,这第一部分的基础理论是必须要学会的,希望大家可以耐心看完. 1.基本概念: 触摸事件 是iOS事件中的一种事件类型,在iOS中按照事件划分 ...
随机推荐
- Unhandled event loop exception GC overhead limit exceeded
1.错误描述 java.lang.OutOfMemoryError: GC overhead limit exceeded at java.util.zip.ZipFile.<init>( ...
- Adobe RIA 开发工程师认证考试大纲
AdobeRIA 开发工程师认证考试大纲 考题数量:共90道题,考试通过正确率:60% 考试时间:120分钟 试题种类:单选题.多选题和判断题 1. Adobe RIA基础知识(2道题) ...
- WRT 版本说明
std_generic 标准通用版nokaid 是不带XBOX支持的(Kai代表连接游戏平台到网络上.)virtual** 表示支持虚拟专用网络的版本VOIP 带VOIP表示支持网络电话的版本.meg ...
- 用VSCode开发一个asp.net core 2.0+angular 5项目(4): Angular5全局错误处理
第一部分: http://www.cnblogs.com/cgzl/p/8478993.html 第二部分: http://www.cnblogs.com/cgzl/p/8481825.html 第三 ...
- Python 学习笔记(二)开发环境的搭建
安装Python windows 下安装: 下载地址:https://www.python.org/downloads Python的版本 3.x 是趋势 2.x 还在被大量使用 至于使用2.x 还是 ...
- Struts2【OGNL、valueStack】就是这么简单
什么是OGNL表达式? OGNL是Object Graphic Navigation Language 是操作对象属性的开源表达式. Struts2框架使用OGNL作为默认的表达式语言. 为什么我们学 ...
- thinkphp在mac下报错
简要:众所周知,开发软件最好的环境是在MAC下;为此在黑苹果上,用Thinkphp开发;在过程中出现,找不到model以及数据库类型错误;为此向大家分享解决办法,希望能够帮助困惑的伙伴们,如果有不对或 ...
- 创建文本节点createTextNode
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 通过smtp直接发送邮件
/// <param name="fromEmail">发件人的邮箱</param> /// <param name="toEmail&qu ...
- 【learning】一种奇妙的网络流建模方式
吐槽 好吧这个是真的很妙qwq用来解方程组的网络流嗯不能更清真 正题 首先是大概描述 当一个方程组中所有的方程相加之后可以把所有的变量都消掉(也就是所有变量都出现一正一负可以抵消掉),我们会发现这个其 ...