转载自:http://www.cnblogs.com/wendingding/p/3871577.html
一、示例
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