IOS-SQLite3的封装
IWStudent.h
//
// IWStudent.h
// 02-SQLite的封装
//
// Created by apple on 14-5-22.
// Copyright (c) 2014年 itcast. All rights reserved.
// #import <Foundation/Foundation.h> @interface IWStudent : NSObject
@property (nonatomic, assign) int ID;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
@end //
// IWStudent.m
// 02-SQLite的封装
//
// Created by apple on 14-5-22.
// Copyright (c) 2014年 itcast. All rights reserved.
// #import "IWStudent.h" @implementation IWStudent @end
IWStudentTool.h
//
// IWStudentTool.h
// 02-SQLite的封装
//
// Created by apple on 14-5-22.
// Copyright (c) 2014年 itcast. All rights reserved.
// 学生数据的CRUD(增删改查) #import <Foundation/Foundation.h>
@class IWStudent; @interface IWStudentTool : NSObject /**
* 添加学生
*
* @param student 需要添加的学生
*/
+ (BOOL)addStudent:(IWStudent *)student; /**
* 获得所有的学生
*
* @return 数组中装着都是IWStudent模型
*/
+ (NSArray *)students; /**
* 根据搜索条件获得对应的学生
*
* @param condition 搜索条件
*/
+ (NSArray *)studentsWithCondition:(NSString *)condition; @end //
// IWStudentTool.m
// 02-SQLite的封装
//
// Created by apple on 14-5-22.
// Copyright (c) 2014年 itcast. All rights reserved.
// #import "IWStudentTool.h"
#import "IWStudent.h"
#import <sqlite3.h> @implementation IWStudentTool // static的作用:能保证_db这个变量只被IWStudentTool.m直接访问
static sqlite3 *_db; + (void)initialize
{
// 0.获得沙盒中的数据库文件名
NSString *filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"student.sqlite"]; // 1.创建(打开)数据库(如果数据库文件不存在,会自动创建)
int result = sqlite3_open(filename.UTF8String, &_db);
if (result == SQLITE_OK) {
NSLog(@"成功打开数据库"); // 2.创表
const char *sql = "create table if not exists t_student (id integer primary key autoincrement, name text, age integer);";
char *errorMesg = NULL;
int result = sqlite3_exec(_db, sql, NULL, NULL, &errorMesg);
if (result == SQLITE_OK) {
NSLog(@"成功创建t_student表");
} else {
NSLog(@"创建t_student表失败:%s", errorMesg);
}
} else {
NSLog(@"打开数据库失败");
}
} + (BOOL)addStudent:(IWStudent *)student
{
NSString *sql = [NSString stringWithFormat:@"insert into t_student (name, age) values('%@', %d);", student.name, student.age]; char *errorMesg = NULL;
int result = sqlite3_exec(_db, sql.UTF8String, NULL, NULL, &errorMesg); return result == SQLITE_OK;
} + (NSArray *)students
{
// 0.定义数组
NSMutableArray *students = nil; // 1.定义sql语句
const char *sql = "select id, name, age from t_student;"; // 2.定义一个stmt存放结果集
sqlite3_stmt *stmt = NULL; // 3.检测SQL语句的合法性
int result = sqlite3_prepare_v2(_db, sql, -, &stmt, NULL);
if (result == SQLITE_OK) {
NSLog(@"查询语句是合法的");
students = [NSMutableArray array]; // 4.执行SQL语句,从结果集中取出数据
while (sqlite3_step(stmt) == SQLITE_ROW) { // 真的查询到一行数据
// 获得这行对应的数据 IWStudent *student = [[IWStudent alloc] init]; // 获得第0列的id
student.ID = sqlite3_column_int(stmt, ); // 获得第1列的name
const unsigned char *sname = sqlite3_column_text(stmt, );
student.name = [NSString stringWithUTF8String:(const char *)sname]; // 获得第2列的age
student.age = sqlite3_column_int(stmt, ); // 添加到数组
[students addObject:student];
}
} else {
NSLog(@"查询语句非合法");
} return students;
} + (NSArray *)studentsWithCondition:(NSString *)condition
{
// 0.定义数组
NSMutableArray *students = nil; // 1.定义sql语句
const char *sql = "select id, name, age from t_student where name like ?;"; // 2.定义一个stmt存放结果集
sqlite3_stmt *stmt = NULL; // 3.检测SQL语句的合法性
int result = sqlite3_prepare_v2(_db, sql, -, &stmt, NULL);
if (result == SQLITE_OK) {
NSLog(@"查询语句是合法的");
students = [NSMutableArray array]; // 填补占位符的内容
NSString *newCondition = [NSString stringWithFormat:@"%%%@%%", condition];
// NSLog(@"%@", newCondition);
sqlite3_bind_text(stmt, , newCondition.UTF8String, -, NULL); // 4.执行SQL语句,从结果集中取出数据
while (sqlite3_step(stmt) == SQLITE_ROW) { // 真的查询到一行数据
// 获得这行对应的数据 IWStudent *student = [[IWStudent alloc] init]; // 获得第0列的id
student.ID = sqlite3_column_int(stmt, ); // 获得第1列的name
const unsigned char *sname = sqlite3_column_text(stmt, );
student.name = [NSString stringWithUTF8String:(const char *)sname]; // 获得第2列的age
student.age = sqlite3_column_int(stmt, ); // 添加到数组
[students addObject:student];
}
} else {
NSLog(@"查询语句非合法");
} return students;
}
@end
IWViewController.m
//
// IWViewController.m
// 01-SQLite的基本使用
//
// Created by apple on 14-5-22.
// Copyright (c) 2014年 itcast. All rights reserved.
// #import "IWViewController.h"
#import "IWStudent.h"
#import "IWStudentTool.h" //Core Data : 苹果官方自带,可以让程序员不用写任何一句SQL
//FMDB @interface IWViewController () <UISearchBarDelegate> /*
- (IBAction)insert;
- (IBAction)update;
- (IBAction)delete;
- (IBAction)query;
*/ @property (nonatomic, strong) NSArray *students;
@end @implementation IWViewController - (NSArray *)students
{
if (_students == nil) {
_students = [IWStudentTool students];
}
return _students;
} - (void)viewDidLoad
{
[super viewDidLoad]; UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(, , , )];
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
} #pragma mark - 搜索框代理
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
self.students = [IWStudentTool studentsWithCondition:searchText];
[self.tableView reloadData];
} #pragma mark - tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.students.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.创建cell
static NSString *ID = @"student";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
} // 2.设置cell的数据
IWStudent *stu = self.students[indexPath.row];
cell.textLabel.text = stu.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", stu.age]; return cell;
} /*
- (IBAction)insert
{
for (int i = 0; i<30; i++) {
// 创建学生
IWStudent *student = [[IWStudent alloc] init];
student.name = [NSString stringWithFormat:@"Jack-%d", arc4random()%100];
student.age = arc4random()%100; // 添加学生
BOOL result = [IWStudentTool addStudent:student]; if (result) {
NSLog(@"添加成功");
}
}
} - (IBAction)update
{ } - (IBAction)delete
{ } - (IBAction)query
{
NSArray *students = [IWStudentTool students]; for (IWStudent *stu in students) {
NSLog(@"%d %@ %d", stu.ID, stu.name, stu.age);
}
}
*/
@end
IOS-SQLite3的封装的更多相关文章
- iOS sqlite3 的基本使用(增 删 改 查)
iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...
- iOS蓝牙原生封装,助力智能硬件开发
代码地址如下:http://www.demodashi.com/demo/12010.html 人工智能自1956年提出以来,一直默默无闻,近年来人工智能的发展得到重视逐渐发展起步,智能硬件.智能手环 ...
- iOS 瀑布流封装
代码地址如下:http://www.demodashi.com/demo/12284.html 一.效果预览 功能描述:WSLWaterFlowLayout 是在继承于UICollectionView ...
- android 仿ios 对话框已封装成工具类
对话框 在android中是一种非经常见的交互提示用户的方式,可是非常多产品狗都叫我们这些做android的仿ios,搞的我们android程序猿非常苦逼,凭什么效果老是仿ios,有没有一点情怀,只是 ...
- iOS中 学会如何对sqlite3 进行封装 (纯手工)
#waring ---(看官注意) ---使用说明: ①在创建自定义model类之前让该类继承自文件中的Model类, ②为model类选一个NSString属性作为主键:(既,在初始化方法里面将从父 ...
- iOS中 学会如何对sqlite3 进行封装
#waring ---(看官注意) ---使用说明: ①在创建自定义model类之前让该类继承自文件中的Model类, ②为model类选一个NSString属性作为主键:(既,在初始化方法里面将从父 ...
- iOS sqlite3数据库解析
看来从版本3.3.1基本上已经支持线程句柄的传递功能.具体限制我标记了一下.(6) Is SQLite threadsafe?SQLite is threadsafe. We make this co ...
- 基于sqlitecpp的sqlite3 c++封装
Github: 人富水也甜 感谢GitHub大佬: sqlitecpp github: https://github.com/SRombauts/SQLiteCpp sqlite: https:// ...
- 【iOS】FMDB封装,查询自动mapping
sqlite几乎所有的App都会用到,但是系统自带的sqlite API是用C语言写的,非常不友好,用起来非常不便,通常我们使用第三方封装好的工具,例如:FMDB(https://github.com ...
- IOS源码封装成.bundle和.a文件,以及加入xib的具体方法,翻遍网络,仅此一家完美翻译!! IOS7!!(3) 完美结局
以上翻译有误解之处,现在简单做法如下: 经过深入研究,才感觉明白了内部机制,现在简单介绍于下,主要步骤:xcode5 创建库项目,删掉测试文件和默认创建的类,添加viewController类带xib ...
随机推荐
- centos7 docker 安装配置
docker快速入门测试 ########################################## #docker安装配置 #环境centos7 #配置docker阿里源 echo '#D ...
- sql server性能调优
转自:https://www.cnblogs.com/woodytu/tag/%E6%80%A7%E8%83%BD%E8%B0%83%E4%BC%98%E5%9F%B9%E8%AE%AD/defaul ...
- Ionic 3 项目的工程目录结构(转载)
工程目录结构说明如下图
- 使用npm构建前端项目基本流程
现在各种前端框架, 库文件基本都托管到npm上, 我们平常下载到别人的项目文件, 也基本是用npm 构建的, 不了解点node和npm那是寸步难行. 下面介绍的代码示例不敢说是最佳实践, 但都是我亲自 ...
- scc
CSS简介 CSS介绍 CSS(cascading style sheet,层叠样式表)是一种制作网页的新技术,现在已经为大多数浏览器所支持,成为网页设计必不可少的工具之一 CSS语法 CSS实例 每 ...
- maven项目中如何创建web.xml
在web工程创建时创建web.xml:用Eclipse新建一个web项目没有自动生成web.xml 在创建maven工程时,大多没有创建web.xml文件,web.xml路径:src/main/web ...
- mybatis入门学习记录(一)
过硬的技术本领,可以给我们保驾护航,飞得更高.今天开始呢.我们就一起来探讨使用mybatis的好处. 首先我们一起来先看看原生的JDBC对于数据库的操作,然后总结其中的利弊,为学习mybatis奠定基 ...
- canvas 事件绑定
Canvas事件绑定 canvas事件绑定 众所周知canvas是位图,在位图里我们可以在里面画各种东西,可以是图片,可以是线条等等.那我们想给canvas里的某一张图片添加一个点击事件该怎么做到 ...
- SQL中的几个判断是否存在
库是否存在if exists(select * from master..sysdatabases wherename=N'库名')print 'exists'elseprint 'not exist ...
- Android应用程序用真机调试步骤
仅供参考: 1.开启调试模式 2.安装 Adb.exe 将platform-tools文件夹里面adb.exe AdbWinApi.dll AdbWinUsbApi.dll拷贝到tools ...