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 ...
随机推荐
- 棣小天儿的第一个python程序
根据给定的年月日,以数字形式打印出日期 months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'Augu ...
- 6.Insert Documents-官方文档摘录
总结 1.插入单文档 db.inventory.insertOne( { item: "canvas", qty: , tags: , w: 35.5, uom: "cm ...
- CNI portmap插件实现源码分析
DNAT创建的iptables规则如下:(重写目的IP和端口) PREROUTING, OUTPUT: --dst-type local -j CNI-HOSTPORT_DNAT // PREROU ...
- 【我的Android进阶之旅】推荐一款能提升数十倍效率的Android应用开发助手
一功能介绍 a调试相关 1布局边界 2布局更新 3强制GPU渲染 4GPU渲染 5指针位置 6严格模式 7不保留应用 8不锁定屏幕 9开发者选项 10系统设置 11语言设置 12USB调试 b UI相 ...
- SQL CHECK sql server免费监控单实例工具
SQL Check 阅读目录 SQL Check? 主要特点 说说不足 下载地址 小结 一款实时性能监测工具 回到目录 SQL Check? 一款实时监测SQL数据库性能.实时排查的问题的免费工具. ...
- 单文件快速体验使用react输出hello_world
看了下react官方的hello world教程, 感觉对新手很不友好.codepen虽然好用, 但是封装太多东西, 看起来 太抽象. 还是喜欢像学习jQuery那样, 直接在单文件中引入必要的js文 ...
- 转:C#访问修饰符
http://www.cnblogs.com/netlyf/archive/2009/12/13/1623103.html
- jsp、freemarker、velocity对比
在java领域.表现层技术主要有三种:jsp.freemarker.velocity. jsp是大家最熟悉的技术长处:1.功能强大,能够写java代码2.支持jsp标签(jsp tag)3.支持表达式 ...
- HDU 6351 (Beautiful Now) 2018 Multi-University Training Contest 5
题意:给定数N(1<=N<=1e9),k(1<=k<=1e9),求对N的任意两位数交换至多k次能得到的最小与最大的数,每一次交换之后不能出现前导零. 因为N最多只有10位,且给 ...
- Set,List,Map的区别
最近在学习struct2中OGNL表达式的过程中,发现自己对set,list,map存在只是欠缺,在百度的过程中发现了此文觉得讲的不错,放到自己博客以便再次查阅,也希望更多地菜鸟看到. java集合的 ...