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的封装的更多相关文章

  1. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  2. iOS蓝牙原生封装,助力智能硬件开发

    代码地址如下:http://www.demodashi.com/demo/12010.html 人工智能自1956年提出以来,一直默默无闻,近年来人工智能的发展得到重视逐渐发展起步,智能硬件.智能手环 ...

  3. iOS 瀑布流封装

    代码地址如下:http://www.demodashi.com/demo/12284.html 一.效果预览 功能描述:WSLWaterFlowLayout 是在继承于UICollectionView ...

  4. android 仿ios 对话框已封装成工具类

    对话框 在android中是一种非经常见的交互提示用户的方式,可是非常多产品狗都叫我们这些做android的仿ios,搞的我们android程序猿非常苦逼,凭什么效果老是仿ios,有没有一点情怀,只是 ...

  5. iOS中 学会如何对sqlite3 进行封装 (纯手工)

    #waring ---(看官注意) ---使用说明: ①在创建自定义model类之前让该类继承自文件中的Model类, ②为model类选一个NSString属性作为主键:(既,在初始化方法里面将从父 ...

  6. iOS中 学会如何对sqlite3 进行封装

    #waring ---(看官注意) ---使用说明: ①在创建自定义model类之前让该类继承自文件中的Model类, ②为model类选一个NSString属性作为主键:(既,在初始化方法里面将从父 ...

  7. iOS sqlite3数据库解析

    看来从版本3.3.1基本上已经支持线程句柄的传递功能.具体限制我标记了一下.(6) Is SQLite threadsafe?SQLite is threadsafe. We make this co ...

  8. 基于sqlitecpp的sqlite3 c++封装

    Github: 人富水也甜 感谢GitHub大佬: sqlitecpp github:  https://github.com/SRombauts/SQLiteCpp sqlite: https:// ...

  9. 【iOS】FMDB封装,查询自动mapping

    sqlite几乎所有的App都会用到,但是系统自带的sqlite API是用C语言写的,非常不友好,用起来非常不便,通常我们使用第三方封装好的工具,例如:FMDB(https://github.com ...

  10. IOS源码封装成.bundle和.a文件,以及加入xib的具体方法,翻遍网络,仅此一家完美翻译!! IOS7!!(3) 完美结局

    以上翻译有误解之处,现在简单做法如下: 经过深入研究,才感觉明白了内部机制,现在简单介绍于下,主要步骤:xcode5 创建库项目,删掉测试文件和默认创建的类,添加viewController类带xib ...

随机推荐

  1. java容器的线程安全性

    参考:https://www.cnblogs.com/yjd_hycf_space/p/7760248.html 线程安全的: Vector HashTable StringBuffer 线程不安全的 ...

  2. JS产品分类列表练习

    CSS: ;;} ul,li{list-style: none;} body{color: #666;background: #f5f5f5;} a{text-decoration: none;col ...

  3. linux执行run文件显示cannot execute binary file

    感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限.错误之处在所难免,欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...

  4. What are the top 10 things that we should be informed about in life

    1.Realize that nobody cares, and if they do, you shouldn't care that they care. Got a new car? Nobod ...

  5. pgadmin3

    一般性  \copyright            显示PostgreSQL的使用和发行许可条款  \g [文件] or;     执行查询 (并把结果写入文件或 |管道)  \gset [PREF ...

  6. 入门拾遗 day2

    一.类和对象 对于Python,一切事物都是对象,对象基于类创建 学会查看帮助 type(类型名) 查看对象的类型dir(类型名) 查看类中提供的所有功能help(类型名) 查看类中所有详细的功能he ...

  7. Java全局变量

    全局变量:Java程序中,不能在所有类之外定义全局变量,只能通过在一个类中定义公用.静态的变量来实现一个全局变量.例如:ClassGlobalVar{public static global_var; ...

  8. [翻译]Feedback on the Go Challenge solutions

    第一次Go Challenge比赛,中国区只有3人参赛. 赛后收到邮件,是一个审阅者的反馈,“Feedback on the Go Challenge solutions”,摘录如下: 保持简单粗暴 ...

  9. redis 笔记05 Sentinel、集群

    Sentinel 1. Sentinel只是一个运行在特殊模式下的Redis服务器,它使用了和普通模式不同的命令表,所以Sentinel模式能够使用的命令和普通的Redis服务器能够使用的命令不同. ...

  10. Linux中显示空闲内存空间的free命令的基本用法

    free 命令显示系统使用和空闲的内存情况,包括物理内存.交互区内存(swap)和内核缓冲区内存 参数 -b 显示内存的单位为字节-k 显示内存的单位为 KB-m 显示内存的单位为 M-o 忽略缓冲区 ...