成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

sqlite實例教程:iOS用sqlite打造詞典

移動開發(fā) iOS
sqlite是個好東西,對于移動平臺來說。一直想寫有關sqlite的教程,但是不知道從何寫起,考慮了很久,還是從一個小Demo 談起吧。我寫了一個精簡版的詞典,實現(xiàn)了增刪查改的基本功能。

sqlite是個好東西,對于移動平臺來說。一直想寫有關sqlite的教程,但是不知道從何寫起,考慮了很久,還是從一個小Demo 談起吧。我寫了一個精簡版的詞典,實現(xiàn)了增刪查改的基本功能。

工程結(jié)構(gòu)如下

最后效果圖如下

效果圖中可以看到,我查詢 "cc",所有相關條目都查詢出來了。

好了,現(xiàn)在開始講解我的項目。首先可以看我的工程目錄,QueryResultList 是界面控制類,DB 是數(shù)據(jù)庫操作類。

整個項目的流程:我們在search框中輸入要查詢的內(nèi)容點擊搜索,底層模糊查詢返回結(jié)果顯示在tableView中。

我們先從底層的操作講起,目前我就實現(xiàn)了插入與查詢操作,刪除與修改以后再補上:

1.創(chuàng)建數(shù)據(jù)庫

  1. - (const char*)getFilePath{//獲取數(shù)據(jù)庫路徑  
  2. return [[NSString stringWithFormat:@"%@/Documents/l",NSHomeDirectory() ] UTF8String];  

  1. // DB.h  
  2. //iukey  
  3. #import <Foundation/Foundation.h>  
  4. #import "/usr/include/sqlite3.h"  
  5. @interface DB : NSObject{  
  6. sqlite3* pdb;//數(shù)據(jù)庫句柄  
  7. }  
  8. @property(nonatomic,assign)sqlite3* pdb;  
  9. - (BOOL)insertRecordWithEN:(NSString*)en CN:(NSString*)cn Comment:(NSString*)comment;//插入一條紀錄  
  10. - (NSMutableArray*)quary:(NSString*)str;//查詢  
  11.  
  12. - (const char*)getFilePath;//獲取數(shù)據(jù)庫路徑  
  13. - (BOOL)createDB;//創(chuàng)建數(shù)據(jù)庫  
  14. - (BOOL)createTable;//創(chuàng)建表  
  15. [url=home.php?mod=space&uid=10695]@END[/url]  

2.創(chuàng)建表

  1. - (BOOL)createTable{  
  2. char* err;  
  3. char* sql = "create table dictionary(ID integer primary key autoincrement,en nvarchar(64),cn nvarchar(128),comment nvarchar(256))";//創(chuàng)建表語句  
  4. if (sql==NULL) {  
  5. return NO;  
  6. }  
  7. if (SQLITE_OK != sqlite3_open([self getFilePath ], &pdb)){  
  8. return NO;  
  9. }  
  10.  
  11. if (SQLITE_OK == sqlite3_exec(pdb, sql, NULL, NULL, &err)) {//執(zhí)行創(chuàng)建表語句成功  
  12. sqlite3_close(pdb);  
  13. return YES;  
  14. }else{//創(chuàng)建表失敗  
  15. return NO;  
  16. }  
  17. }  

3.插入一條紀錄

  1. - (BOOL)insertRecordWithEN:(NSString*)en CN:(NSString*)cn Comment:(NSString*)comment{  
  2. int ret = 0;  
  3. if (SQLITE_OK != sqlite3_open([self getFilePath ], &pdb)){//打開數(shù)據(jù)庫  
  4. return NO;  
  5. }  
  6. char* sql = "insert into dictionary(en,cn,comment) values(?,?,?);";//插入語句,3個參數(shù)  
  7. sqlite3_stmt* stmt;//  
  8. if (sqlite3_prepare_v2(pdb, sql, -1, &stmt, nil)==SQLITE_OK) {//準備語句  
  9. sqlite3_bind_text(stmt, 1, [en UTF8String], -1, NULL);//綁定參數(shù)  
  10. sqlite3_bind_text(stmt, 2, [cn UTF8String], -1, NULL);  
  11. sqlite3_bind_text(stmt, 3, [comment UTF8String], -1, NULL);  
  12. }else{  
  13. return NO;  
  14. }  
  15. if (SQLITE_DONE == (ret = sqlite3_step(stmt))) {//執(zhí)行查詢  
  16. sqlite3_finalize(stmt);  
  17. sqlite3_close(pdb);  
  18. return YES;  
  19. }else{  
  20. return NO;  
  21. }  
  22. }  

4.查詢

  1. - (NSMutableArray*)quary:(NSString *)str{  
  2. NSMutableArray* arr =[[NSMutableArray alloc]init];//存放查詢結(jié)果  
  3. if (SQLITE_OK != sqlite3_open([self getFilePath ], &pdb)){  
  4. return NO;  
  5. }  
  6. char* sql = "select * from dictionary where en like ? or cn like ? or comment like ?;";//查詢語句  
  7. sqlite3_stmt* stmt;  
  8. if (sqlite3_prepare_v2(pdb, sql, -1, &stmt, nil)==SQLITE_OK) {//準備  
  9. sqlite3_bind_text(stmt, 1,[[NSString stringWithFormat:@"%%%@%%",str]UTF8String], -1, NULL);  
  10. sqlite3_bind_text(stmt, 2, [[NSString stringWithFormat:@"%%%@%%",str]UTF8String], -1, NULL);  
  11. sqlite3_bind_text(stmt, 3, [[NSString stringWithFormat:@"%%%@%%",str]UTF8String], -1, NULL);  
  12. }else{  
  13. return nil;  
  14. }  
  15. while( SQLITE_ROW == sqlite3_step(stmt) ){//執(zhí)行  
  16. char* _en = (char*)sqlite3_column_text(stmt, 1);  
  17. char* _cn = (char*)sqlite3_column_text(stmt, 2);  
  18. char* _comment = (char*)sqlite3_column_text(stmt, 3);  
  19.  
  20. NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];//單條紀錄  
  21. [dict setObject:[NSString stringWithCString:_en encoding:NSUTF8StringEncoding] forKey:@"kEN"];  
  22. [dict setObject:[NSString stringWithCString:_cn encoding:NSUTF8StringEncoding] forKey:@"kCN"];  
  23. [dict setObject:[NSString stringWithCString:_comment encoding:NSUTF8StringEncoding] forKey:@"kCOMMENT"];  
  24. [arr addObject:dict];//插入到結(jié)果數(shù)組  
  25. }  
  26. sqlite3_finalize(stmt);  
  27. sqlite3_close(pdb);  
  28. return [arr autorelease];//返回查詢結(jié)果數(shù)組  
  29. }  

5.DB 初始化

我先定義了一個宏,用來標識是否是第一次運行程序,如果是第一次運行就要運行創(chuàng)建數(shù)據(jù)庫與表的函數(shù),否則就不運行,具體看代碼:

  1. #define FIRSTINIT 1//第一次運行則設為1,否則就是0  
  2. - (id)init{  
  3. self = [super init];  
  4. if (self!=nil) {  
  5. #if FIRSTINIT  
  6. [self createDB];  
  7. [self createTable];  
  8. [self insertRecordWithEN:@"cctv1" CN:@"央視1套" Comment:@"SB電視臺1"];//為了方便測試我插入了一些紀錄  
  9. [self insertRecordWithEN:@"cctv2" CN:@"央視2套" Comment:@"SB電視臺2"];  
  10. [self insertRecordWithEN:@"cctv3" CN:@"央視3套" Comment:@"SB電視臺3"];  
  11. [self insertRecordWithEN:@"cctv4" CN:@"央視4套" Comment:@"SB電視臺4"];  
  12. [self insertRecordWithEN:@"cctv5" CN:@"央視5套" Comment:@"SB電視臺5"];  
  13. [self insertRecordWithEN:@"cctv6" CN:@"央視6套" Comment:@"SB電視臺6"];  
  14. [self insertRecordWithEN:@"cctv7" CN:@"央視7套" Comment:@"SB電視臺7"];  
  15. [self insertRecordWithEN:@"cctv8" CN:@"央視8套" Comment:@"SB電視臺8"];  
  16. [self insertRecordWithEN:@"cctv9" CN:@"央視9套" Comment:@"SB電視臺9"];  
  17. [self insertRecordWithEN:@"cctv10" CN:@"央視10套" Comment:@"SB電視臺10"];  
  18. [self insertRecordWithEN:@"cctv11" CN:@"央視11套" Comment:@"SB電視臺11"];  
  19. [self insertRecordWithEN:@"cctv12" CN:@"央視12套" Comment:@"SB電視臺12"];  
  20. #endif  
  21. }  
  22. return self;  
  23. }  

底層的數(shù)據(jù)庫暫時就這些,接著講上層的界面部分

  1. // QueryResultList.h  
  2. // iukey  
  3.  
  4. #import <UIKit/UIKit.h>  
  5. #import "DB.h"  
  6.  
  7. @interface QueryResultList : UITableViewController<UISearchBarDelegate>{  
  8. NSMutableArray* mArr;//tableView數(shù)據(jù)源  
  9. DB* db ;//數(shù)據(jù)庫對象  
  10. UISearchBar* searchBar ;//搜索框  
  11. }  
  12. @property(nonatomic,retain)NSMutableArray* mArr;  
  13. @property(nonatomic,retain)DB* db;  
  14. @property(nonatomic,retain)UISearchBar* searchBar ;  
  15. @end 
  1. - (id)initWithStyle:(UITableViewStyle)style{  
  2. self = [super initWithStyle:style];  
  3. if (self) {  
  4. mArr = [[NSMutableArray alloc]init];//表數(shù)據(jù)源  
  5. db =[[DB alloc]init];//數(shù)據(jù)庫控制器  
  6. searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(44.0,0,200.0,44)];//搜索控件  
  7. searchBar.delegate=self;//設置搜索控件的委托  
  8. self.navigationItem.titleView = searchBar;  
  9. }  
  10. return self;  
  11. }  

接下來我們實現(xiàn)表格數(shù)據(jù)源委托

  1. #pragma mark - Table view data source  
  2.  
  3. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
  4. return 1;//分區(qū)數(shù)  
  5. }  
  6.  
  7. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  8. return [mArr count];//行數(shù)  
  9. }  
  10.  
  11. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  12. static NSString *CellIdentifier = @"Cell";  
  13.  
  14. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  15. for ( UIView* view in cell.contentView.subviews) {  
  16. [view removeFromSuperview];  
  17. }  
  18.  
  19. if (cell == nil) {  
  20. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
  21. }  
  22. UILabel* lblEN = [[UILabel alloc]initWithFrame:CGRectMake(5.0, 5.0, 300.0, 30.0)];//顯示英文的文字標簽控件  
  23. UILabel* lblCN = [[UILabel alloc]initWithFrame:CGRectMake(5.0, 35.0, 300.0, 30.0)];//中文  
  24. UILabel* lblComment = [[UILabel alloc]initWithFrame:CGRectMake(5.0, 65.0, 300.0, 30.0)];//詳細  
  25.  
  26. //背景顏色清掉  
  27. lblEN.backgroundColor = [UIColor clearColor];  
  28. lblCN.backgroundColor = [UIColor clearColor];  
  29. lblComment.backgroundColor = [UIColor clearColor];  
  30. //  
  31. lblEN.text = [[mArr objectAtIndex:indexPath.row] objectForKey:@"kEN"];  
  32. lblCN.text = [[mArr objectAtIndex:indexPath.row] objectForKey:@"kCN"];  
  33. lblComment.text = [[mArr objectAtIndex:indexPath.row] objectForKey:@"kCOMMENT"];  
  34.  
  35. [cell.contentView addSubview:lblEN];  
  36. [cell.contentView addSubview:lblCN];  
  37. [cell.contentView addSubview:lblComment];  
  38.  
  39. cell.selectionStyle = UITableViewCellSelectionStyleNone;//選中不要高亮  
  40. [lblEN release];  
  41. [lblCN release];  
  42. [lblComment release];  
  43.  
  44. return cell;  
  45. }  

然后實現(xiàn)搜索委托方法:

  1. #pragma mark - UISearchBar delegate  
  2. - (void) searchBarSearchButtonClicked:(UISearchBar*)activeSearchbar{  
  3. [mArr removeAllObjects];  
  4. NSString* query= searchBar.text;  
  5. NSMutableArray* arr = [db quary:query];  
  6. for ( NSMutableDictionary* dict in arr) {  
  7. [mArr addObject:dict];  
  8. }  
  9. [searchBar resignFirstResponder];  
  10. [self.tableView reloadData];  
  11. }  

基本就結(jié)束了,這只是一個簡單的Demo,sqlite的基本使用方法我也會慢慢整理出來。

責任編輯:閆佳明 來源: apkbus
相關推薦

2012-03-06 09:13:04

Android SQLSQLiteAndroid

2009-09-18 11:44:05

Scala實例教程Kestrel

2021-02-15 15:40:28

SQLite3數(shù)據(jù)庫

2014-08-26 11:46:46

QtAndroid實例教程

2013-05-03 13:42:20

iOS開發(fā)SQLite3存儲讀取

2012-03-06 12:59:11

iOS SQLite3iOSSQLite3

2013-04-09 15:49:04

iOSSQLite基礎內(nèi)容簡

2010-08-17 11:02:45

DIV CSS實例教程

2019-06-17 15:25:17

expandunexpandLinux

2012-02-29 10:18:31

SQLite3Android

2011-07-05 17:54:43

QT Sqlite ARM

2011-07-05 14:46:34

2011-07-25 16:03:47

XCode 編譯

2013-01-06 09:52:43

SQLite

2013-04-09 16:04:06

iOS開發(fā)SQLite知識總結(jié)

2013-01-04 16:17:33

Android開發(fā)圖像特效圖像處理

2009-09-08 14:18:35

NFS服務器

2011-09-02 19:12:59

IOS應用Sqlite數(shù)據(jù)庫

2011-07-04 17:26:00

Qt SQLite

2011-07-26 18:11:56

iPhone Sqlite 數(shù)據(jù)庫
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品久久久久久一级毛片 | 成人精品国产免费网站 | 手机在线一区二区三区 | 一区二区电影 | 麻豆视频在线免费看 | 精品在线一区 | 亚洲精品视频免费 | 在线观看视频一区二区三区 | 鲁大师一区影视 | 国产精品久久久久国产a级 欧美日韩国产免费 | 99久热在线精品视频观看 | 伊人导航 | 国产精品久久久久一区二区三区 | 亚洲第一中文字幕 | 中文字幕不卡在线88 | 欧美mv日韩mv国产网站91进入 | 国产精品中文字幕一区二区三区 | 午夜免费影视 | 亚洲国产成人精品久久久国产成人一区 | 欧美一级做a爰片免费视频 国产美女特级嫩嫩嫩bbb片 | 久久蜜桃资源一区二区老牛 | 国产一区二区三区在线视频 | 韩日在线视频 | 天天艹天天干天天 | 一区二区在线 | av片在线观看 | 日韩三级| 鸳鸯谱在线观看高清 | 国产免费一区二区三区网站免费 | 久久一久久 | 欧美一区二区二区 | 在线免费观看视频你懂的 | 国产精品久久二区 | 精品一区二区三区四区在线 | 精品国产高清一区二区三区 | 91免费福利视频 | 久久一| 99视频在线免费观看 | 91精品久久久久久综合五月天 | 亚洲一在线 | 欧美电影大全 |