IWStudent.h

  1. //
  2. // IWStudent.h
  3. // 02-SQLite的封装
  4. //
  5. // Created by apple on 14-5-22.
  6. // Copyright (c) 2014年 itcast. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface IWStudent : NSObject
  12. @property (nonatomic, assign) int ID;
  13. @property (nonatomic, copy) NSString *name;
  14. @property (nonatomic, assign) int age;
  15. @end
  16.  
  17. //
  18. // IWStudent.m
  19. // 02-SQLite的封装
  20. //
  21. // Created by apple on 14-5-22.
  22. // Copyright (c) 2014年 itcast. All rights reserved.
  23. //
  24.  
  25. #import "IWStudent.h"
  26.  
  27. @implementation IWStudent
  28.  
  29. @end
  1. IWStudentTool.h
  1. //
  2. // IWStudentTool.h
  3. // 02-SQLite的封装
  4. //
  5. // Created by apple on 14-5-22.
  6. // Copyright (c) 2014年 itcast. All rights reserved.
  7. // 学生数据的CRUD(增删改查)
  8.  
  9. #import <Foundation/Foundation.h>
  10. @class IWStudent;
  11.  
  12. @interface IWStudentTool : NSObject
  13.  
  14. /**
  15. * 添加学生
  16. *
  17. * @param student 需要添加的学生
  18. */
  19. + (BOOL)addStudent:(IWStudent *)student;
  20.  
  21. /**
  22. * 获得所有的学生
  23. *
  24. * @return 数组中装着都是IWStudent模型
  25. */
  26. + (NSArray *)students;
  27.  
  28. /**
  29. * 根据搜索条件获得对应的学生
  30. *
  31. * @param condition 搜索条件
  32. */
  33. + (NSArray *)studentsWithCondition:(NSString *)condition;
  34.  
  35. @end
  36.  
  37. //
  38. // IWStudentTool.m
  39. // 02-SQLite的封装
  40. //
  41. // Created by apple on 14-5-22.
  42. // Copyright (c) 2014年 itcast. All rights reserved.
  43. //
  44.  
  45. #import "IWStudentTool.h"
  46. #import "IWStudent.h"
  47. #import <sqlite3.h>
  48.  
  49. @implementation IWStudentTool
  50.  
  51. // static的作用:能保证_db这个变量只被IWStudentTool.m直接访问
  52. static sqlite3 *_db;
  53.  
  54. + (void)initialize
  55. {
  56. // 0.获得沙盒中的数据库文件名
  57. NSString *filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"student.sqlite"];
  58.  
  59. // 1.创建(打开)数据库(如果数据库文件不存在,会自动创建)
  60. int result = sqlite3_open(filename.UTF8String, &_db);
  61. if (result == SQLITE_OK) {
  62. NSLog(@"成功打开数据库");
  63.  
  64. // 2.创表
  65. const char *sql = "create table if not exists t_student (id integer primary key autoincrement, name text, age integer);";
  66. char *errorMesg = NULL;
  67. int result = sqlite3_exec(_db, sql, NULL, NULL, &errorMesg);
  68. if (result == SQLITE_OK) {
  69. NSLog(@"成功创建t_student表");
  70. } else {
  71. NSLog(@"创建t_student表失败:%s", errorMesg);
  72. }
  73. } else {
  74. NSLog(@"打开数据库失败");
  75. }
  76. }
  77.  
  78. + (BOOL)addStudent:(IWStudent *)student
  79. {
  80. NSString *sql = [NSString stringWithFormat:@"insert into t_student (name, age) values('%@', %d);", student.name, student.age];
  81.  
  82. char *errorMesg = NULL;
  83. int result = sqlite3_exec(_db, sql.UTF8String, NULL, NULL, &errorMesg);
  84.  
  85. return result == SQLITE_OK;
  86. }
  87.  
  88. + (NSArray *)students
  89. {
  90. // 0.定义数组
  91. NSMutableArray *students = nil;
  92.  
  93. // 1.定义sql语句
  94. const char *sql = "select id, name, age from t_student;";
  95.  
  96. // 2.定义一个stmt存放结果集
  97. sqlite3_stmt *stmt = NULL;
  98.  
  99. // 3.检测SQL语句的合法性
  100. int result = sqlite3_prepare_v2(_db, sql, -, &stmt, NULL);
  101. if (result == SQLITE_OK) {
  102. NSLog(@"查询语句是合法的");
  103. students = [NSMutableArray array];
  104.  
  105. // 4.执行SQL语句,从结果集中取出数据
  106. while (sqlite3_step(stmt) == SQLITE_ROW) { // 真的查询到一行数据
  107. // 获得这行对应的数据
  108.  
  109. IWStudent *student = [[IWStudent alloc] init];
  110.  
  111. // 获得第0列的id
  112. student.ID = sqlite3_column_int(stmt, );
  113.  
  114. // 获得第1列的name
  115. const unsigned char *sname = sqlite3_column_text(stmt, );
  116. student.name = [NSString stringWithUTF8String:(const char *)sname];
  117.  
  118. // 获得第2列的age
  119. student.age = sqlite3_column_int(stmt, );
  120.  
  121. // 添加到数组
  122. [students addObject:student];
  123. }
  124. } else {
  125. NSLog(@"查询语句非合法");
  126. }
  127.  
  128. return students;
  129. }
  130.  
  131. + (NSArray *)studentsWithCondition:(NSString *)condition
  132. {
  133. // 0.定义数组
  134. NSMutableArray *students = nil;
  135.  
  136. // 1.定义sql语句
  137. const char *sql = "select id, name, age from t_student where name like ?;";
  138.  
  139. // 2.定义一个stmt存放结果集
  140. sqlite3_stmt *stmt = NULL;
  141.  
  142. // 3.检测SQL语句的合法性
  143. int result = sqlite3_prepare_v2(_db, sql, -, &stmt, NULL);
  144. if (result == SQLITE_OK) {
  145. NSLog(@"查询语句是合法的");
  146. students = [NSMutableArray array];
  147.  
  148. // 填补占位符的内容
  149. NSString *newCondition = [NSString stringWithFormat:@"%%%@%%", condition];
  150. // NSLog(@"%@", newCondition);
  151. sqlite3_bind_text(stmt, , newCondition.UTF8String, -, NULL);
  152.  
  153. // 4.执行SQL语句,从结果集中取出数据
  154. while (sqlite3_step(stmt) == SQLITE_ROW) { // 真的查询到一行数据
  155. // 获得这行对应的数据
  156.  
  157. IWStudent *student = [[IWStudent alloc] init];
  158.  
  159. // 获得第0列的id
  160. student.ID = sqlite3_column_int(stmt, );
  161.  
  162. // 获得第1列的name
  163. const unsigned char *sname = sqlite3_column_text(stmt, );
  164. student.name = [NSString stringWithUTF8String:(const char *)sname];
  165.  
  166. // 获得第2列的age
  167. student.age = sqlite3_column_int(stmt, );
  168.  
  169. // 添加到数组
  170. [students addObject:student];
  171. }
  172. } else {
  173. NSLog(@"查询语句非合法");
  174. }
  175.  
  176. return students;
  177. }
  178. @end
  1. IWViewController.m
  1. //
  2. // IWViewController.m
  3. // 01-SQLite的基本使用
  4. //
  5. // Created by apple on 14-5-22.
  6. // Copyright (c) 2014年 itcast. All rights reserved.
  7. //
  8.  
  9. #import "IWViewController.h"
  10. #import "IWStudent.h"
  11. #import "IWStudentTool.h"
  12.  
  13. //Core Data : 苹果官方自带,可以让程序员不用写任何一句SQL
  14. //FMDB
  15.  
  16. @interface IWViewController () <UISearchBarDelegate>
  17.  
  18. /*
  19. - (IBAction)insert;
  20. - (IBAction)update;
  21. - (IBAction)delete;
  22. - (IBAction)query;
  23. */
  24.  
  25. @property (nonatomic, strong) NSArray *students;
  26. @end
  27.  
  28. @implementation IWViewController
  29.  
  30. - (NSArray *)students
  31. {
  32. if (_students == nil) {
  33. _students = [IWStudentTool students];
  34. }
  35. return _students;
  36. }
  37.  
  38. - (void)viewDidLoad
  39. {
  40. [super viewDidLoad];
  41.  
  42. UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(, , , )];
  43. searchBar.delegate = self;
  44. self.tableView.tableHeaderView = searchBar;
  45. }
  46.  
  47. #pragma mark - 搜索框代理
  48. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  49. {
  50. self.students = [IWStudentTool studentsWithCondition:searchText];
  51. [self.tableView reloadData];
  52. }
  53.  
  54. #pragma mark - tableView代理方法
  55. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  56. {
  57. return self.students.count;
  58. }
  59.  
  60. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  61. {
  62. // 1.创建cell
  63. static NSString *ID = @"student";
  64. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  65. if (cell == nil) {
  66. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
  67. }
  68.  
  69. // 2.设置cell的数据
  70. IWStudent *stu = self.students[indexPath.row];
  71. cell.textLabel.text = stu.name;
  72. cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", stu.age];
  73.  
  74. return cell;
  75. }
  76.  
  77. /*
  78. - (IBAction)insert
  79. {
  80. for (int i = 0; i<30; i++) {
  81. // 创建学生
  82. IWStudent *student = [[IWStudent alloc] init];
  83. student.name = [NSString stringWithFormat:@"Jack-%d", arc4random()%100];
  84. student.age = arc4random()%100;
  85.  
  86. // 添加学生
  87. BOOL result = [IWStudentTool addStudent:student];
  88.  
  89. if (result) {
  90. NSLog(@"添加成功");
  91. }
  92. }
  93. }
  94.  
  95. - (IBAction)update
  96. {
  97.  
  98. }
  99.  
  100. - (IBAction)delete
  101. {
  102.  
  103. }
  104.  
  105. - (IBAction)query
  106. {
  107. NSArray *students = [IWStudentTool students];
  108.  
  109. for (IWStudent *stu in students) {
  110. NSLog(@"%d %@ %d", stu.ID, stu.name, stu.age);
  111. }
  112. }
  113. */
  114. @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. 棣小天儿的第一个python程序

    根据给定的年月日,以数字形式打印出日期 months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'Augu ...

  2. 6.Insert Documents-官方文档摘录

    总结 1.插入单文档 db.inventory.insertOne( { item: "canvas", qty: , tags: , w: 35.5, uom: "cm ...

  3. CNI portmap插件实现源码分析

    DNAT创建的iptables规则如下:(重写目的IP和端口) PREROUTING, OUTPUT: --dst-type local -j CNI-HOSTPORT_DNAT  // PREROU ...

  4. 【我的Android进阶之旅】推荐一款能提升数十倍效率的Android应用开发助手

    一功能介绍 a调试相关 1布局边界 2布局更新 3强制GPU渲染 4GPU渲染 5指针位置 6严格模式 7不保留应用 8不锁定屏幕 9开发者选项 10系统设置 11语言设置 12USB调试 b UI相 ...

  5. SQL CHECK sql server免费监控单实例工具

    SQL Check 阅读目录 SQL Check? 主要特点 说说不足 下载地址 小结 一款实时性能监测工具 回到目录 SQL Check? 一款实时监测SQL数据库性能.实时排查的问题的免费工具. ...

  6. 单文件快速体验使用react输出hello_world

    看了下react官方的hello world教程, 感觉对新手很不友好.codepen虽然好用, 但是封装太多东西, 看起来 太抽象. 还是喜欢像学习jQuery那样, 直接在单文件中引入必要的js文 ...

  7. 转:C#访问修饰符

    http://www.cnblogs.com/netlyf/archive/2009/12/13/1623103.html

  8. jsp、freemarker、velocity对比

    在java领域.表现层技术主要有三种:jsp.freemarker.velocity. jsp是大家最熟悉的技术长处:1.功能强大,能够写java代码2.支持jsp标签(jsp tag)3.支持表达式 ...

  9. HDU 6351 (Beautiful Now) 2018 Multi-University Training Contest 5

    题意:给定数N(1<=N<=1e9),k(1<=k<=1e9),求对N的任意两位数交换至多k次能得到的最小与最大的数,每一次交换之后不能出现前导零. 因为N最多只有10位,且给 ...

  10. Set,List,Map的区别

    最近在学习struct2中OGNL表达式的过程中,发现自己对set,list,map存在只是欠缺,在百度的过程中发现了此文觉得讲的不错,放到自己博客以便再次查阅,也希望更多地菜鸟看到. java集合的 ...