SQLite3系统学习【5】SQLite模糊查询

前端之家收集整理的这篇文章主要介绍了SQLite3系统学习【5】SQLite模糊查询前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转载自:http://www.cnblogs.com/wendingding/p/3871577.html

一、示例

说明:本文简单示例了sqlite的模糊查询

1.新建一个继承自NSObject的模型

该类中的代码

 1 //
@H_301_25@ 2 //  YYPerson.h
@H_301_25@ 3   03-模糊查询
@H_301_25@ 4 @H_301_25@ 5   Created by apple on 14-7-27.
@H_301_25@ 6   Copyright (c) 2014年 wendingding. All rights reserved.
@H_301_25@ 7 //
@H_301_25@ 8 
@H_301_25@ 9 #import <Foundation/Foundation.h>
@H_301_25@10 
@H_301_25@11 @interface YYPerson : NSObject
@H_301_25@12 @property (nonatomic,assign) int ID;
@H_301_25@13 @property (nonatomic,copy) NSString *name;
@H_301_25@14 @property (nonatomic,255); line-height:1.5!important">int age;
@H_301_25@15 
@H_301_25@16 @end

2.新建一个工具类,用来管理模型

工具类中的代码设计如下:

YYPersonTool.h文件

YYPersonTool.h @class YYPerson; @H_301_25@12 @interface YYPersonTool : NSObject @H_301_25@13 /** @H_301_25@14 * 保存一个联系人 @H_301_25@15 */ @H_301_25@16 + (void)save:( YYPerson*)person; @H_301_25@17 @H_301_25@18 @H_301_25@19 * 查询所有的联系人 @H_301_25@20 @H_301_25@21 + (NSArray *)query; @H_301_25@22 + (NSArray *)queryWithCondition:(NSString *)condition; @H_301_25@23

YYPersonTool.m文件

1 @H_301_25@ 2 YYPersonTool.m @H_301_25@ 3 @H_301_25@ 4 @H_301_25@ 5 @H_301_25@ 6 @H_301_25@ 7 @H_301_25@ 8 @H_301_25@ 9 #import "YYPersonTool.h" @H_301_25@ 10 YYPerson.h 11 @H_301_25@ 12 #import <sqlite3.h> @H_301_25@ 13 @interface YYPersonTool () @H_301_25@ 14 @property(nonatomic,assign)sqlite3 *db; @H_301_25@ 15 @end @H_301_25@ 16 @implementation YYPersonTool @H_301_25@ 17 @H_301_25@ 18 static sqlite3 *_db; @H_301_25@ 19 首先需要有数据库 @H_301_25@ 20 +(void)initialize @H_301_25@ 21 { @H_301_25@ 22 获得数据库文件的路径 @H_301_25@ 23 NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject]; @H_301_25@ 24 NSString *fileName=[doc stringByAppendingPathComponent:@"person.sqlite"]; @H_301_25@ 25 将OC字符串转换为c语言的字符串 @H_301_25@ 26 const char *cfileName=fileName.UTF8String; @H_301_25@ 27 @H_301_25@ 28 1.打开数据库文件(如果数据库文件不存在,那么该函数自动创建数据库文件 @H_301_25@ 29 int result = sqlite3_open(cfileName,&_db); @H_301_25@ 30 if (result==sqlITE_OK) { 打开成功 @H_301_25@ 31 NSLog(成功打开数据库"); @H_301_25@ 32 @H_301_25@ 33 2.创建表 @H_301_25@ 34 char *sql=CREATE TABLE IF NOT EXISTS t_person (id integer PRIMARY KEY AUTOINCREMENT,name text NOT NULL,age integer NOT NULL);"; @H_301_25@ 35 @H_301_25@ 36 char *errmsg=NULL; @H_301_25@ 37 result = sqlite3_exec(_db,sql,NULL,&errmsg); @H_301_25@ 38 if (result==sqlITE_OK) { @H_301_25@ 39 NSLog(创表成功 40 }else @H_301_25@ 41 { @H_301_25@ 42 printf(创表失败---%s",errmsg); @H_301_25@ 43 } @H_301_25@ 44 }@H_301_25@ 45 { @H_301_25@ 46 NSLog(打开数据库失败 47 } @H_301_25@ 48 @H_301_25@ 49 } @H_301_25@ 50 保存一条数据 @H_301_25@ 51 +(void)save:(YYPerson *)person @H_301_25@ 52 { @H_301_25@ 53 1.拼接sql语句 @H_301_25@ 54 @H_301_25@ 55 NSString *sql=[NSString stringWithFormat:INSERT INTO t_person (name,age) VALUES ('%@',%d);301_25@ 56 @H_301_25@ 57 2.执行sql语句 @H_301_25@ 58 @H_301_25@ 59 sqlite3_exec(_db,sql.UTF8String,128); line-height:1.5!important"> 60 if (errmsg) {如果有错误信息 @H_301_25@ 61 NSLog(插入数据失败--%s 62 }@H_301_25@ 63 { @H_301_25@ 64 NSLog(插入数据成功 65 } @H_301_25@ 66 @H_301_25@ 67 } @H_301_25@ 68 @H_301_25@ 69 +(NSArray *)query @H_301_25@ 70 { @H_301_25@ 71 return [self queryWithCondition:@""]; @H_301_25@ 72 } @H_301_25@ 73 @H_301_25@ 74 模糊查询 @H_301_25@ 75 +(NSArray *)queryWithCondition:(NSString *)condition @H_301_25@ 76 { @H_301_25@ 77 @H_301_25@ 78 数组,用来存放所有查询到的联系人 @H_301_25@ 79 NSMutableArray *persons=nil; @H_301_25@ 80 /* @H_301_25@ 81 [NSString stringWithFormat:@"SELECT id,name,age FROM t_person WHERE name like '%%%@%%' ORDER BY age ASC;",condition]; @H_301_25@ 82 NSString *NSsql=[NSString stringWithFormat:@"SELECT id,age FROM t_person WHERE name=%@;",128); line-height:1.5!important"> 83 */ @H_301_25@ 84 NSString *NSsql=[NSString stringWithFormat:SELECT id,age FROM t_person WHERE name like '%%%@%%' ORDER BY age ASC;301_25@ 85 NSLog(%@sql); @H_301_25@ 86 char *sql=NSsql.UTF8String; @H_301_25@ 87 @H_301_25@ 88 sqlite3_stmt *stmt=NULL; @H_301_25@ 89 @H_301_25@ 90 进行查询前的准备工作 @H_301_25@ 91 if (sqlite3_prepare_v2(_db,-1,&stmt,NULL)==sqlITE_OK) {sql语句没有问题 @H_301_25@ 92 NSLog(查询语句没有问题 93 @H_301_25@ 94 persons=[NSMutableArray array]; @H_301_25@ 95 @H_301_25@ 96 调用一次sqlite3_step函数,stmt就会指向下一条记录 @H_301_25@ 97 while (sqlite3_step(stmt)==sqlITE_ROW) {找到一条记录 @H_301_25@ 98 @H_301_25@ 99 取出数据 @H_301_25@100 (1)取出第0列字段的值(int类型的值) @H_301_25@101 int ID=sqlite3_column_int(stmt,0); @H_301_25@102 (2)取出第1列字段的值(text类型的值) @H_301_25@103 const unsigned char *name=sqlite3_column_text(stmt,128); line-height:1.5!important">1); @H_301_25@104 (3)取出第2列字段的值(int类型的值) @H_301_25@105 int age=sqlite3_column_int(stmt,128); line-height:1.5!important">2); @H_301_25@106 @H_301_25@107 YYPerson *p=[[YYPerson alloc]init]; @H_301_25@108 p.ID=ID; @H_301_25@109 p.name=[NSString stringWithUTF8String:(char *)name]; @H_301_25@110 p.age=age; @H_301_25@111 NSLog(@"%@",p.name); @H_301_25@112 [persons addObject:p]; @H_301_25@113 NSLog(@"haha%@",persons); @H_301_25@114 } @H_301_25@115 }@H_301_25@116 { @H_301_25@117 NSLog(查询语句有问题118 } @H_301_25@119 @H_301_25@120 NSLog(@"haha%@",128); line-height:1.5!important">121 return persons; @H_301_25@122 } @H_301_25@123

3.在storyboard中,删除原有的控制器,放一个导航控制器和UITableViewController控制器,并关联

代码中,让主控制器直接继承自UITableViewController

代码设计如下:

YYViewController.m文件

YYViewController.m YYViewController.h10 @H_301_25@12 @interface YYViewController ()<UISearchBarDelegate> @H_301_25@14 @H_301_25@15 添加一个数组,用来保存person @H_301_25@16 @property(nonatomic,strong)NSArray *persons; @H_301_25@17 @H_301_25@18 @implementation YYViewController @H_301_25@20 @H_301_25@21 #pragma mark-懒加载 @H_301_25@22 -(NSArray *)persons @H_301_25@23 { @H_301_25@24 if (_persons==nil) { @H_301_25@25 _persons=[YYPersonTool query]; @H_301_25@26 } @H_301_25@27 return _persons; @H_301_25@28 } @H_301_25@29 @H_301_25@30 1.在初始化方法添加一个搜索 @H_301_25@31 - (void)viewDidLoad @H_301_25@32 { @H_301_25@33 [super viewDidLoad]; @H_301_25@34 @H_301_25@35 设置搜索 @H_301_25@36 UISearchBar *search=[[UISearchBar alloc]init]; @H_301_25@37 search.frame=CGRectMake(0,128); line-height:1.5!important">300,128); line-height:1.5!important">44); @H_301_25@38 search.delegate=self; @H_301_25@39 self.navigationItem.titleView=search; @H_301_25@40 } @H_301_25@41 @H_301_25@42 2.设置tableView的数据 @H_301_25@43 设置有多少行数据 @H_301_25@44 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section @H_301_25@45 { @H_301_25@46 return 10; @H_301_25@47 return self.persons.count; @H_301_25@48 } @H_301_25@49 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath @H_301_25@50 { @H_301_25@51 1.去缓存中取cll,若没有则自己创建并标记 @H_301_25@52 static NSString *ID=ID53 UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID]; @H_301_25@54 if (cell==nil) { @H_301_25@55 cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; @H_301_25@56 } @H_301_25@57 @H_301_25@58 2.设置每个cell的数据 @H_301_25@59 先取出数据模型 @H_301_25@60 YYPerson *person=self.persons[indexPath.row]; @H_301_25@61 设置这个cell的姓名(name)和年龄 @H_301_25@62 cell.textLabel.text=person.name; @H_301_25@63 cell.detailTextLabel.text=[NSString stringWithFormat:年龄 %d64 3.返回cell @H_301_25@65 return cell; @H_301_25@66 } @H_301_25@67 @H_301_25@68 - (IBAction)add:(UIBarButtonItem *)sender { @H_301_25@69 初始化一些假数据 @H_301_25@70 NSArray *names = @[西门抽血",0); line-height:1.5!important">西门抽筋西门抽风西门吹雪东门抽血东门抽筋东门抽风东门吹雪北门抽血北门抽筋南门抽风南门吹雪71 for (int i = 0; i<20; i++) { @H_301_25@72 YYPerson *p = [[YYPerson alloc] init]; @H_301_25@73 p.name = [NSString stringWithFormat:%@-%d100)]; @H_301_25@74 p.age = arc4random_uniform(20) + 20; @H_301_25@75 [YYPersonTool save:p]; @H_301_25@76 } @H_301_25@77 } @H_301_25@78 @H_301_25@79 #pragma mark-搜索框的代理方法 @H_301_25@80 -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText @H_301_25@81 { @H_301_25@82 self.persons=[YYPersonTool queryWithCondition:searchText]; @H_301_25@83 刷新表格 @H_301_25@84 [self.tableView reloadData]; @H_301_25@85 [searchBar resignFirstResponder]; @H_301_25@86 } @H_301_25@87 @H_301_25@88

实现效果

  

二、简单说明

关于:NSString*NSsql=[NSStringstringWithFormat:@"SELECT id,condition];
注意:name like ‘西门’,相当于是name = ‘西门’。
name like ‘%西%’,为模糊搜索搜索字符串中间包含了’西’,左边可以为任意字符串,右边可以为任意字符串,的字符串。
但是在stringWithFormat:中%是转义字符,两个%才表示一个%。
打印查看:

猜你在找的Sqlite相关文章