加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-12 19:49:05 所属栏目:百科 来源:网络整理
导读:转载自:http://www.cnblogs.com/wendingding/p/3871577.html 一、示例 说明:本文简单示例了SQLite的模糊查询 1.新建一个继承自NSObject的模型 该类中的代码: 1 // 2 // YYPerson.h 3 03-模糊查询 4 5 Created by apple on 14-7-27. 6 Copyright (c) 2014

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

一、示例

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

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

该类中的代码:

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

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

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

YYPersonTool.h文件

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

YYPersonTool.m文件

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

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

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

代码设计如下:

YYViewController.m文件

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

实现效果:

  

二、简单说明

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读