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中按照事件划分 ...
随机推荐
- 安装JBoss Tool 出错
安装JBoss Tool 出错 具体报错如下:
- 从DataTable中查询数据
/// <summary> /// 从DataTable中查询数据 /// </summary> /// <param name="tb">待处 ...
- Excle巧取汉字打头的字串内容
处理表格数据时遇到问题:从网页表格中复制来的数据,地址一列中出现类似于“4AWZCX万载老林业局2”的无用字符前缀.现在希望提取第一个汉字及其之后的所有内容(图1),寻求高效分离中英文及数字的实操方法 ...
- asp.net core轻松入门之MVC中Options读取配置文件
接上一篇中讲到利用Bind方法读取配置文件 ASP.NET Core轻松入门Bind读取配置文件到C#实例 那么在这篇文章中,我将在上一篇文章的基础上,利用Options方法读取配置文件 首先注册MV ...
- Error Correct System CodeForces - 527B
Ford Prefect got a job as a web developer for a small company that makes towels. His current work ta ...
- SpringMVC拦截器(包括自定以拦截器--实现HandlerInterceptorAdapter)(资源和权限管理)
一,springmvc的配置 <!-- 访问拦截 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping ...
- json 的循环输出
json不能用for-of循环,会报错 可以用for-in循环: var json = {'a':'apple','b':'banana','c':'orange','d':'pear'}; for( ...
- 如何降低90%Java垃圾回收时间?以阿里HBase的GC优化实践为例
过去的一年里,我们准备在Ali-HBase上突破这个被普遍认知的痛点,为此进行了深度分析及全面创新的工作,获得了一些比较好的效果.以蚂蚁风控场景为例,HBase的线上young GC时间从120ms减 ...
- 设置mysql密码 Access denied 问题
原文:http://www.upwqy.com/details/31.html 在Mac上安装完mysql以后 在终端执行 /usr/local/mysql/bin/mysql 可以直接进入.但是在设 ...
- js实现最长子串算法
var arr=["weeweadbshow","jhsaasrbgddbshow","ccbshow"]; function Longes ...