大佬教程收集整理的这篇文章主要介绍了sqlite 实例教程 IOS下用sqlite打造词典-IOS开发,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
声明
欢迎转载,但是请尊重作者劳动成果,转载请保留此框内声明,谢谢。文章出处:http://blog.csdn.net/iukey |
sqlite 是个好东西,对于移动平台来说。一直想写有关sqlite的教程,但是不知道从何写起,考虑了很久,还是从一个小Demo 谈起吧。我写了一个精简版的词典,实现了增删查改的基本功能。
工程结构如下。最后效果图如下。
效果图中可以看到,我查询 "cc",所有相关条目都查询出来了。
好了,现在开始讲解我的项目。首先可以看我的工程目录,queryResultList 是界面控制类,DB 是数据库操作类。
整个项目的流程:我们在search框中输入要查询的内容点击搜索,底层模糊查询返回结果显示在tableVIEw中。
我们先从底层的操作讲起,目前我就实现了插入与查询操作,删除与修改以后再补上:
1.创建数据库
- (const char*)getfilePath{//获取数据库路径 return [[NsString StringWithFormat:@"%@/documents/l",NSHomeDirectory() ] UTF8String]; }
// DB.h //iukey #import <Foundation/Foundation.h> #import "/usr/include/sqlite3.h" @interface DB : NSObject{ sqlite3* pdb;//数据库句柄 } @property(nonatomic,assign)sqlite3* pdb; - (BOol)insertRecorDWithEN:(NsString*)en CN:(NsString*)cn Comment:(NsString*)comment;//插入一条纪录 - (NSMutableArray*)quary:(NsString*)str;//查询 - (const char*)getfilePath;//获取数据库路径 - (BOol)createdb;//创建数据库 - (BOol)createtable;//创建表 @end2.创建表
- (BOol)createtable{ char* err; char* sql = "create table Dictionary(ID INTEGER PRIMary key autoincrement,en nvarchar(64),cn nvarchar(128),comment nvarchar(256))";//创建表语句 if (sql==NulL) { return NO; } if (@R_452_5607@OK != sqlite3_open([self getfilePath ],&pdb)){ return NO; } if (@R_452_5607@OK == sqlite3_exec(pdb,sql,NulL,&err)) {//执行创建表语句成功 sqlite3_close(pdb); return YES; }else{//创建表失败 return NO; } }3.插入一条纪录
- (BOol)insertRecorDWithEN:(NsString*)en CN:(NsString*)cn Comment:(NsString*)comment{ int ret = 0; if (@R_452_5607@OK != sqlite3_open([self getfilePath ],&pdb)){//打开数据库 return NO; } char* sql = "insert into Dictionary(en,cn,comment) values(?,?,?);";//插入语句,3个参数 sqlite3_stmt* stmt;// if (sqlite3_prepare_v2(pdb,-1,&stmt,nil)==@R_452_5607@OK) {//准备语句 sqlite3_bind_text(stmt,1,[en UTF8String],null);//绑定参数 sqlite3_bind_text(stmt,2,[cn UTF8String],null); sqlite3_bind_text(stmt,3,[comment UTF8String],null); }else{ return NO; } if (@R_452_5607@DONE == (ret = sqlite3_step(stmt))) {//执行查询 sqlite3_finalize(stmt); sqlite3_close(pdb); return YES; }else{ return NO; } }4.查询
- (NSMutableArray*)quary:(NsString *)str{ NSMutableArray* arr =[[NSMutableArray alloc]init];//存放查询结果 if (@R_452_5607@OK != sqlite3_open([self getfilePath ],&pdb)){ return NO; } char* sql = "SELEct * from Dictionary where en like ? or cn like ? or comment like ?;";//查询语句 sqlite3_stmt* stmt; if (sqlite3_prepare_v2(pdb,nil)==@R_452_5607@OK) {//准备 sqlite3_bind_text(stmt,[[NsString StringWithFormat:@"%%%@%%",str]UTF8String],null); }else{ return nil; } while( @R_452_5607@ROW == sqlite3_step(stmt) ){//执行 char* _en = (char*)sqlite3_column_text(stmt,1); char* _cn = (char*)sqlite3_column_text(stmt,2); char* _comment = (char*)sqlite3_column_text(stmt,3); NSMutableDictionary* Dict = [[NSMutableDictionary alloc]init];//单条纪录 [Dict setobject:[NsString StringWithCString:_en enCoding:NSUTF8StringEnCoding] forKey:@"kEN"]; [Dict setobject:[NsString StringWithCString:_cn enCoding:NSUTF8StringEnCoding] forKey:@"kCN"]; [Dict setobject:[NsString StringWithCString:_comment enCoding:NSUTF8StringEnCoding] forKey:@"kCOMMENT"]; [arr addobject:Dict];//插入到结果数组 } sqlite3_finalize(stmt); sqlite3_close(pdb); return [arr autorelease];//返回查询结果数组 }5.DB 初始化
我先定义了一个宏,用来标识是否是第一次运行程序,如果是第一次运行就要运行创建数据库与表的函数,否则就不运行,具体看代码:
#define FirsTiniT 1//第一次运行则设为1,否则就是0 - (ID)init{ self = [super init]; if (self!=nil) { #if FirsTiniT [self createdb]; [self createtable]; [self insertRecorDWithEN:@"cctv1" CN:@"央视1套" Comment:@"SB电视台1"];//为了方便测试我插入了一些纪录 [self insertRecorDWithEN:@"cctv2" CN:@"央视2套" Comment:@"SB电视台2"]; [self insertRecorDWithEN:@"cctv3" CN:@"央视3套" Comment:@"SB电视台3"]; [self insertRecorDWithEN:@"cctv4" CN:@"央视4套" Comment:@"SB电视台4"]; [self insertRecorDWithEN:@"cctv5" CN:@"央视5套" Comment:@"SB电视台5"]; [self insertRecorDWithEN:@"cctv6" CN:@"央视6套" Comment:@"SB电视台6"]; [self insertRecorDWithEN:@"cctv7" CN:@"央视7套" Comment:@"SB电视台7"]; [self insertRecorDWithEN:@"cctv8" CN:@"央视8套" Comment:@"SB电视台8"]; [self insertRecorDWithEN:@"cctv9" CN:@"央视9套" Comment:@"SB电视台9"]; [self insertRecorDWithEN:@"cctv10" CN:@"央视10套" Comment:@"SB电视台10"]; [self insertRecorDWithEN:@"cctv11" CN:@"央视11套" Comment:@"SB电视台11"]; [self insertRecorDWithEN:@"cctv12" CN:@"央视12套" Comment:@"SB电视台12"]; #endif } return self; }底层的数据库暂时就这些,接着讲上层的界面部分
// queryResultList.h // iukey #import <UIKit/UIKit.h> #import "DB.h" @interface queryResultList : UItableVIEwController<UISearchbarDelegate>{ NSMutableArray* mArr;//tableVIEw数据源 DB* db ;//数据库对象 UISearchbar* searchbar ;//搜索框 } @property(nonatomic,retain)NSMutableArray* mArr; @property(nonatomic,retain)DB* db; @property(nonatomic,retain)UISearchbar* searchbar ; @end
- (ID)initWithStyle:(UItableVIEwStylE)style{ self = [super initWithStyle:style]; if (self) { mArr = [[NSMutableArray alloc]init];//表数据源 db =[[DB alloc]init];//数据库控制器 searchbar = [[UISearchbar alloc]initWithFrame:CGRectMake(44.0,200.0,44)];//搜索控件 searchbar.delegate=self;//设置搜索控件的委托 self.navigationItem.titleVIEw = searchbar; } return self; }接下来我们实现表格数据源委托
#pragma mark - table vIEw data source - (NSInteger)numberOfSectionsIntableVIEw:(UItableVIEw *)tableVIEw{ return 1;//分区数 } - (NSInteger)tableVIEw:(UItableVIEw *)tableVIEw numberOfRowsInSection:(NSInteger)section{ return [mArr count];//行数 } - (UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw cellForRowATindexPath:(NSIndexPath *)indexPath{ static NsString *CellIDentifIEr = @"Cell"; UItableVIEwCell *cell = [tableVIEw dequeueReusableCellWithIDentifIEr:CellIDentifIEr]; for ( UIVIEw* vIEw in cell.contentVIEw.subvIEws) { [vIEw removeFromSupervIEw]; } if (cell == nil) { cell = [[[UItableVIEwCell alloc] initWithStyle:UItableVIEwCellStyleDefault reusEIDentifIEr:CellIDentifIEr] autorelease]; } UILabel* lblEN = [[UILabel alloc]initWithFrame:CGRectMake(5.0,5.0,300.0,30.0)];//显示英文的文字标签控件 UILabel* lblCN = [[UILabel alloc]initWithFrame:CGRectMake(5.0,35.0,30.0)];//中文 UILabel* lblComment = [[UILabel alloc]initWithFrame:CGRectMake(5.0,65.0,30.0)];//详细 //背景颜色清掉 lblEN.BACkgroundcolor = [UIcolor clearcolor]; lblCN.BACkgroundcolor = [UIcolor clearcolor]; lblComment.BACkgroundcolor = [UIcolor clearcolor]; // lblEN.text = [[mArr objectATindex:indexPath.row] objectForKey:@"kEN"]; lblCN.text = [[mArr objectATindex:indexPath.row] objectForKey:@"kCN"]; lblComment.text = [[mArr objectATindex:indexPath.row] objectForKey:@"kCOMMENT"]; [cell.contentVIEw addSubvIEw:lblEN]; [cell.contentVIEw addSubvIEw:lblCN]; [cell.contentVIEw addSubvIEw:lblComment]; cell.SELEctionStyle = UItableVIEwCellSELEctionStyleNone;//选中不要高亮 [lblEN release]; [lblCN release]; [lblComment release]; return cell; }然后实现搜索委托方法:
#pragma mark - UISearchbar delegate - (voID) searchbarSearchbuttonClicked:(UISearchbar*)activeSearchbar{ [mArr removeAllObjects]; NsString* query= searchbar.text; NSMutableArray* arr = [db quary:query]; for ( NSMutableDictionary* Dict in arr) { [mArr addobject:Dict]; } [searchbar resignFirstResponder]; [self.tableVIEw reloadData]; }基本就结束了,这只是一个简单的Demo,sqlite的基本使用方法我也会慢慢整理出来,最后附上完整工程文件: DictionaryDemo
以上是大佬教程为你收集整理的sqlite 实例教程 IOS下用sqlite打造词典-IOS开发全部内容,希望文章能够帮你解决sqlite 实例教程 IOS下用sqlite打造词典-IOS开发所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。