iOS数据持久化(二)SQLite
一、什么是SQLite
#import <Foundation/Foundation.h>
#import "Student.h"
@interface DataBaseHandle : NSObject
+ (DataBaseHandle *)shareDataBase;
- (void)openDB;
- (void)closeDB;
//添加数据
- (void)insertNewStudent:(Student *)student;
/**
* 根据学号查询学生
*/
- (Student *)selectStudentWithNumber:(NSInteger)number;
/**
* 查询表中所有数据
*/
- (NSMutableArray *)selectAllStudents;
/**
* 根据学号删除
*/
- (void)deleteStudentWithNumber:(NSInteger)number;
- (void)updateStudent:(NSString *)gender WithNumber:(NSInteger)number;
@end
DataBaseHandle.m
+ (DataBaseHandle *)shareDataBase {
@synchronized (self){
if (handle == nil) {
handle = [[DataBaseHandle alloc] init];
// [handle closeDB];
}
}
return handle;
}
sqlite3 *db = nil;
打开数据库
- (void)openDB {
NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
NSString *path = [str stringByAppendingPathComponent:@"student.sqlite"];
NSLog(@"%@",path);
//打开数据库
//UTF8String 将oc字符串转化为C语言字符串
//方法执行完会返回一个数据库对象,这个对象已被初始化
int result = sqlite3_open([path UTF8String], &db);
//如果等于SQLITE_OK说明sql语句执行成功
if (result == SQLITE_OK) {
NSLog(@"数据库打开成功");
//创建表格
NSString *sqlString = @"create table if not exists Student (number integer primary key autoincrement,name text,gender text,age integer)";
int result = sqlite3_exec(db, [sqlString UTF8String], NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"见表成功");
}
} else {
NSLog(@"数据库打开失败");
}
}
关闭数据库
- (void)closeDB {
int result = sqlite3_close(db);
if (result == SQLITE_OK) {
NSLog(@"关闭成功");
}else {
NSLog(@"关闭失败");
}
}
插入数据
- (void)insertNewStudent:(Student *)student {
[self openDB];
//准备sql语句
NSString *sqlString = @"insert into Student (name,gender, age) values (?, ?, ?)";
/*第一个参数,数据库指针,
第二个参数,sql语句
第三个参数,sql语句的长度,写成-1,自动计算
第四个参数,创建管理sql语句的类,statement
第五个参数,预留参数
*/
sqlite3_stmt *stmt = nil;
int result = sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &stmt, NULL);
if (result == SQLITE_OK) {
NSLog(@"添加语句成功");
//绑定参数
//绑定的参数:1.管理类指针,2.第几个问号,3.绑定的数据, 4.绑定数据的长度 -1 5.
sqlite3_bind_text(stmt, 1, [student.name UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 2, [student.gender UTF8String], -1, NULL);
sqlite3_bind_int(stmt, 3, (int)student.age);
/**/
sqlite3_step(stmt);
} else {
NSLog(@"添加语句失败");
}
sqlite3_finalize(stmt);
[self closeDB];
}
查询数据
- (NSMutableArray *)selectAllStudents {
[self openDB];
NSString *sqlString = @"select * from student";
sqlite3_stmt *stmt = nil;
int result = sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &stmt, nil);
if (result == SQLITE_OK) {
NSLog(@"查询全部成功");
//循环的条件:下一行还有数据,这时就能一直循环下去
NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
while (sqlite3_step(stmt) == SQLITE_ROW) {
NSString *name = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
NSString *gender = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
NSInteger age = sqlite3_column_int(stmt, 3);
Student *stu = [[Student alloc] init];
stu.name = name;
stu.age = age;
stu.gender = gender;
[array addObject:stu];
[stu release];
}
sqlite3_finalize(stmt);
[self closeDB];
return array;
} else {
NSLog(@"error");
return nil;
}
}
- (Student *)selectStudentWithNumber:(NSInteger)number {
[self openDB];
NSString *sqlString = @"select * from Student where number = ?";
sqlite3_stmt *stmt = nil;
int result = sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &stmt, NULL);
if (result == SQLITE_OK) {
NSLog(@"查询成功");
//绑定参数
sqlite3_bind_int(stmt, 1, (int)number);
Student *student = [[[Student alloc] init]autorelease];
while (sqlite3_step(stmt) == SQLITE_ROW) {
student.name = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
student.gender = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
student.age = sqlite3_column_int(stmt, 3);
}
sqlite3_finalize(stmt);
[self closeDB];
return student;
} else {
NSLog(@"不OK");
return nil;
}
}
删除数据
- (void)deleteStudentWithNumber:(NSInteger)number {
[self openDB];
NSString *sqlString = @"delete from Student where number = ?";
sqlite3_stmt *stmt = nil;
int result = sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &stmt, NULL);
if (result == SQLITE_OK) {
sqlite3_bind_int(stmt, 1, (int)number);
//执行sql语句
sqlite3_step(stmt);
}
//释放stmt的内存资源
sqlite3_finalize(stmt);
[self closeDB];
}
修改数据
- (void)updateStudent:(NSString *)gender WithNumber:(NSInteger)number {
[self openDB];
NSString *sqlString = @"update Student set gender = ? where number = ?";
sqlite3_stmt *stmt = nil;
int result = sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &stmt, NULL);
if (result == SQLITE_OK) {
sqlite3_bind_int(stmt, 2, (int)number);
sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
[self closeDB];
}
iOS数据持久化(二)SQLite的更多相关文章
- IOS数据持久化之归档NSKeyedArchiver
IOS数据持久化的方式分为三种: 属性列表 (自定义的Property List .NSUserDefaults) 归档 (NSKeyedArchiver) 数据库 (SQLite.Core Data ...
- iOS -数据持久化方式-以真实项目讲解
前面已经讲解了SQLite,FMDB以及CoreData的基本操作和代码讲解(CoreData也在不断学习中,上篇博客也会不断更新中).本篇我们将讲述在实际开发中,所使用的iOS数据持久化的方式以及怎 ...
- iOS 数据持久化(扩展知识:模糊背景效果和密码保护功能)
本篇随笔除了介绍 iOS 数据持久化知识之外,还贯穿了以下内容: (1)自定义 TableView,结合 block 从 ViewController 中分离出 View,轻 ViewControll ...
- iOS开发笔记-swift实现iOS数据持久化之归档NSKeyedArchiver
IOS数据持久化的方式分为三种: 属性列表 (plist.NSUserDefaults) 归档 (NSKeyedArchiver) 数据库 (SQLite.Core Data.第三方类库等 归档(又名 ...
- iOS数据持久化方式及class_copyIvarList与class_copyPropertyList的区别
iOS数据持久化方式:plist文件(属性列表)preference(偏好设置)NSKeyedArchiver(归档)SQLite3CoreData沙盒:iOS程序默认情况下只能访问自己的程序目录,这 ...
- iOS数据持久化-OC
沙盒详解 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文 ...
- iOS数据持久化
在iOS中,实现数据持久化一般分为4大种: 1.属性列表 2.对象归档 3.SQLite 4.Core Data 一.属性列表 NSUserDefaults类的使用和NSKeyedArchiver有很 ...
- iOS: 数据持久化方案
数据持久化方案(如果总结不到位,或者有误的地方,敬请斧正) 一.功能: 主要是将数据持久化到本地,减少对网络请求的次数,既节省了用户的流量,也增强了App的体验效果. 二.种类: plist存储:使 ...
- iOS数据持久化存储:归档
在平时的iOS开发中,我们经常用到的数据持久化存储方式大概主要有:NSUserDefaults(plist),文件,数据库,归档..前三种比较经常用到,第四种归档我个人感觉用的还是比较少的,恰恰因为用 ...
- 转载 -- iOS数据持久化存储
作者:@翁呀伟呀 授权本站转载 概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方 ...
随机推荐
- Codeforces Round #313 (Div. 1) A. Gerald's Hexagon 数学题
A. Gerald's Hexagon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559/p ...
- This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms. 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分 解决方案
但web启用了md5加密后 有可能出现这样的错误 This implementation is not part of the Windows Platform FIPS validated cryp ...
- 使用AmplifyJS和JQuery编写更好更优雅的javascript事件处理代码
事件(或消息)是一种经常使用的软件设计模式.可以减少消息处理者和消息公布者的之间的耦合,比方J2EE里面的JMS规范.设计模式中的观察者模式(也叫公布/订阅模式).这对于javascript代码相同适 ...
- 有趣的动画视图集合:Android View Animations
Android View Animations这个项目收集了各种有趣的动画效果. 所有效果: Attension Flash, Pulse, RubberBand, Shake, Swing, Wob ...
- iis7.5配置.net mvc注意事项
iis7.5配置.net mvc注意事项 1. 应用程序池采用经典模式,framework4.0.可能存在权限问题,解决办法:在高级设置的标识设为LocalSystem.一般mvc都采用集成模式, ...
- python类型转换、数值操作(转)
最近学习python语言,碰到数据类型间的转换问题.看到一篇文章总结的挺详细,收藏之备用. 类型转换 代码 Code highlighting produced by Actipro CodeHigh ...
- MySQL高可用之MHA的搭建 转
http://www.cnblogs.com/muhu/p/4045780.html http://www.cnblogs.com/gomysql/p/3675429.html http://www ...
- Linux内存管理学习笔记 转
https://yq.aliyun.com/articles/11192?spm=0.0.0.0.hq1MsD 随着要维护的服务器增多,遇到的各种稀奇古怪的问题也会增多,要想彻底解决这些“小”问题往往 ...
- LeetCode38 Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- spring WebServiceTemplate 调用 axis1.4 发布的webservice
前言: 最近在开发中需要调用对方的 webservice服务,按照现有的技术,本应该是一件很简单的事情,只需要拿到wsdl文件,生成客户端代码即可,但是,对方的webservice服务是06年用ax ...