上都海奶茶馆 发表于 2016-11-29 10:10:13

sqlite 实例教程 IOS下用sqlite打造词典-IOS开发

声明
欢迎转载,但是请尊重作者劳动成果,转载请保留此框内声明,谢谢。
文章出处:http://blog.csdn.net/iukey  sqlite 是个好东西,对于移动平台来说。一直想写有关sqlite的教程,但是不知道从何写起,考虑了很久,还是从一个小Demo 谈起吧。我写了一个精简版的词典,实现了增删查改的基本功能。
  工程结构如下。最后效果图如下。
  效果图中可以看到,我查询 "cc",所有相关条目都查询出来了。
  好了,现在开始讲解我的项目。首先可以看我的工程目录,QueryResultList 是界面控制类,DB 是数据库操作类。
  整个项目的流程:我们在search框中输入要查询的内容点击搜索,底层模糊查询返回结果显示在tableView中。
  我们先从底层的操作讲起,目前我就实现了插入与查询操作,删除与修改以后再补上:
  1.创建数据库
- (const char*)getFilePath{//获取数据库路径return [ 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 (SQLITE_OK != sqlite3_open(, &pdb)){return NO;}if (SQLITE_OK == sqlite3_exec(pdb, sql, NULL, 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 (SQLITE_OK != sqlite3_open(, &pdb)){//打开数据库return NO;}char* sql = "insert into dictionary(en,cn,comment) values(?,?,?);";//插入语句,3个参数sqlite3_stmt* stmt;//if (sqlite3_prepare_v2(pdb, sql, -1, &stmt, nil)==SQLITE_OK) {//准备语句sqlite3_bind_text(stmt, 1, , -1, NULL);//绑定参数sqlite3_bind_text(stmt, 2, , -1, NULL);sqlite3_bind_text(stmt, 3, , -1, NULL);}else{return NO;}if (SQLITE_DONE == (ret = sqlite3_step(stmt))) {//执行查询sqlite3_finalize(stmt);sqlite3_close(pdb);return YES;}else{return NO;}}4.查询- (NSMutableArray*)quary:(NSString *)str{NSMutableArray* arr =[init];//存放查询结果if (SQLITE_OK != sqlite3_open(, &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, sql, -1, &stmt, nil)==SQLITE_OK) {//准备sqlite3_bind_text(stmt, 1,[UTF8String], -1, NULL);sqlite3_bind_text(stmt, 2, [UTF8String], -1, NULL);sqlite3_bind_text(stmt, 3, [UTF8String], -1, NULL);}else{return nil;}while( SQLITE_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 = [init];//单条纪录 forKey:@"kEN"]; forKey:@"kCN"]; forKey:@"kCOMMENT"];;//插入到结果数组}sqlite3_finalize(stmt);sqlite3_close(pdb);return ;//返回查询结果数组}5.DB 初始化  我先定义了一个宏,用来标识是否是第一次运行程序,如果是第一次运行就要运行创建数据库与表的函数,否则就不运行,具体看代码:
#define FIRSTINIT 1//第一次运行则设为1,否则就是0- (id)init{self = ;if (self!=nil) {#if FIRSTINIT;;;//为了方便测试我插入了一些纪录;;;;;;;;;;;#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 = ;if (self) {mArr= [init];//表数据源db =[init];//数据库控制器searchBar = [initWithFrame:CGRectMake(44.0,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 ;//行数}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString *CellIdentifier = @"Cell";UITableViewCell *cell = ;for ( UIView* view in cell.contentView.subviews) {;}if (cell == nil) {cell = [[ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];}UILabel* lblEN = [initWithFrame:CGRectMake(5.0, 5.0, 300.0, 30.0)];//显示英文的文字标签控件UILabel* lblCN = [initWithFrame:CGRectMake(5.0, 35.0, 300.0, 30.0)];//中文UILabel* lblComment = [initWithFrame:CGRectMake(5.0, 65.0, 300.0, 30.0)];//详细//背景颜色清掉lblEN.backgroundColor = ;lblCN.backgroundColor = ;lblComment.backgroundColor = ;//lblEN.text = [ objectForKey:@"kEN"];lblCN.text = [ objectForKey:@"kCN"];lblComment.text = [ objectForKey:@"kCOMMENT"];;;;cell.selectionStyle = UITableViewCellSelectionStyleNone;//选中不要高亮;;;return cell;}然后实现搜索委托方法:#pragma mark - UISearchBar delegate- (void) searchBarSearchButtonClicked:(UISearchBar*)activeSearchbar{;NSString* query= searchBar.text;NSMutableArray* arr = ;for ( NSMutableDictionary* dict in arr) {;};;}基本就结束了,这只是一个简单的Demo,sqlite的基本使用方法我也会慢慢整理出来,最后附上完整工程文件:DictionaryDemo





  

  
页: [1]
查看完整版本: sqlite 实例教程 IOS下用sqlite打造词典-IOS开发