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. MySQL之表的约束

    一 介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY ...

  2. rest_framework之魔法类

    视图 from rest_framework.viewsets import ViewSetMixin # 注意先后顺序,ViewSetMixin写在前面 class TestAll(ViewSetM ...

  3. tornado web应用程序结构

    tornado web 应用程序通常包含一个或者多个RequestHandler 子类,一个Application 对象来为每个控制器路由到达的请求和一个mian()函数 import tornado ...

  4. DRF(1) - REST、DRF(View源码解读、APIView源码解读)

    一.REST 1.什么是编程? 数据结构和算法的结合. 2.什么是REST? 首先回顾我们曾经做过的图书管理系统,我们是这样设计url的,如下: /books/ /get_all_books/ 访问所 ...

  5. Android图片加载框架Picasso最全使用教程4

    通过前几篇的学习,我们已经对Picasso的加载图片的用法有了很深的了解,接下来我们开始分析Picasso为我们提供的其他高级功能及内存分析,Let’sGo ! Picasso进行图片的旋转(Rota ...

  6. OpenOCD SWD调试stm32f0

    参考:http://www.stmcu.org.cn/module/forum/thread-610998-1-2.html

  7. PAT 天梯赛 L1-004. 计算摄氏温度 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-004 AC代码 #include <iostream> #include <cstdio&g ...

  8. hadoop27---netty中handler的执行顺序

    Netty是基于Java NIO的网络应用框架. Netty是一个NIO client-server(客户端服务器)框架,使用Netty可以快速开发网络应用,例如服务器和客户端协议.Netty提供了一 ...

  9. Apache 虚拟主机配置

    开放虚拟主机文件 修改主配置文件 解开注释,使用虚拟主机配置文件. vim /usr/local/apache2/conf/httpd.conf Include conf/extra/httpd-vh ...

  10. JavaWeb JavaScript

    1.JavaScript概述 JavaScript是一种基于对象和事件驱动的脚本语言,原名叫做livescript.W3c组织开发的标准叫ECMAscipt 1.1JavaScript和Java的一些 ...