成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

sqlite操作與封裝的示例分析

小編給大家分享一下sqlite操作與封裝的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)長期為上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為虹口企業(yè)提供專業(yè)的網(wǎng)站制作、成都做網(wǎng)站虹口網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

ocsqlite.h

[plain]
view plaincopy

  1. //  

  2. //  OCSqlite.m  

  3. //  sqlite  

  4. //  

  5. //  Created by fengsh on 12-12-3.  

  6. //  Copyright (c) 2012年 sqlite_Lib. All rights reserved.  

  7. //  

  8. /*  

  9.  對SQLITE的封裝,主要設(shè)計是操作上的習(xí)慣。對于輕量級數(shù)據(jù)量可以。  

  10.  對于在數(shù)據(jù)量就需要注意內(nèi)存的開銷了。  

  11.  */  

  12. #import <Foundation/Foundation.h>  

  13. #import <sqlite3.h>  

  14. enum fieldtype  

  15. {  

  16.     ftInt,ftFloat,ftDouble,ftString,ftBlob,ftBool,ftDate,ftTime,ftDateTime,ftBinary  

  17. };  

  18. /*  

  19.  字段類  

  20.  作用:主要用于與數(shù)據(jù)庫中的字段屬性進(jìn)行對應(yīng)  

  21.  字段名,字段類型,字段值,字段索引號  

  22.  */  

  23. @interface OCField : NSObject   

  24. {      

  25.     NSString* fieldName;  

  26.     id fieldValue;  

  27.     enum fieldtype mtype;  

  28.     int seq_column;  

  29. }  

  30. -(NSString*)toString;  

  31. -(NSInteger)toInteger;  

  32. -(NSDate*)toDate;  

  33. -(NSString*)toDateString;  

  34. -(NSString*)toTimeString;  

  35. -(NSString*)toDateTimeString;  

  36. -(NSNumber*)toNumber;  

  37. -(enum fieldtype)getFieldType;  

  38. @property (nonatomic) int seq_column;  

  39. @end  

  40. /*  

  41.  數(shù)據(jù)集類  

  42.  作用:  

  43.  類似于數(shù)據(jù)源的集合,帶游標(biāo),可訪問數(shù)據(jù)源中的數(shù)據(jù)  

  44.  */  

  45. @interface OCDataset : NSObject  

  46. {  

  47.     NSMutableArray* records;  

  48.     NSInteger cursor;  

  49. }  

  50. -(void)clear;  

  51. -(NSInteger)count;  

  52. -(BOOL)next;  

  53. -(BOOL)first;  

  54. -(BOOL)move:(NSInteger) index;  

  55. -(OCField*)fieldbyname:(NSString*) fieldname;  

  56. -(OCField*)indexOffield:(NSInteger) index;  

  57. @end  

  58. /*  

  59. 簡單的數(shù)據(jù)定義語言操作及數(shù)據(jù)庫查詢的封裝  

  60.  未支持參數(shù)綁定,因此在處理blob上還需要擴(kuò)展代碼。  

  61.  后續(xù)完善  

  62.  */  

  63. @interface OCSqlite : NSObject  

  64. {  

  65.     sqlite3* db;  

  66.     OCDataset* dataset;  

  67. }  

  68. -(id)init;  

  69. -(BOOL)ConnectToDB:(NSString*) dbfilepath;  

  70. -(void)DisconnectDB;  

  71. -(BOOL)startTranslation;  

  72. -(BOOL)commitTranslation;  

  73. -(BOOL)rollbackTranslation;  

  74. -(BOOL)excesql:(NSString*) ddlsql;  

  75. -(BOOL)query:(NSString*) qysql;  

  76. @property (nonatomic,readonly) OCDataset* dataset;  

  77. @end  

ocsqlite.m

[plain]
view plaincopy

  1. //  

  2. //  OCSqlite.m  

  3. //  sqlite  

  4. //  

  5. //  Created by fengsh on 12-12-3.  

  6. //  Copyright (c) 2012年 sqlite_Lib. All rights reserved.  

  7. //  

  8. #import "OCSqlite.h"  

  9. @implementation OCField  

  10. @synthesize seq_column;  

  11. -(id)init  

  12. {  

  13.     self = [super init];  

  14.     if (self) {  

  15.         fieldValue = NULL;  

  16.         return self;  

  17.     }  

  18.     return nil;  

  19. }  

  20. -(void)setfield:(NSString*) name withvalue:(sqlite3_value*) value withtype:(NSString*) tp  

  21. {  

  22.     fieldName = name;  

  23.     NSString* result = @"";  

  24.     if ([tp isEqualToString:@"integer"]||[tp isEqualToString:@"smallint"])  

  25.     {  

  26.         mtype = ftInt;  

  27.         fieldValue = (id)sqlite3_value_int(value);  

  28.         return;  

  29.     }  

  30.     else if ([tp isEqualToString:@"boolean"])  

  31.     {  

  32.         mtype = ftBool;  

  33.     }  

  34.     else if ([tp isEqualToString:@"float"])  

  35.     {  

  36.         mtype = ftFloat;  

  37.     }  

  38.     else if ([tp isEqualToString:@"double"]||[tp isEqualToString:@"real"])  

  39.     {  

  40.         mtype = ftDouble;  

  41.     }  

  42.     else if ([tp isEqualToString:@"text"]||[tp isEqualToString:@"varchar"])  

  43.     {  

  44.         mtype = ftString;  

  45.     }  

  46.     else if ([tp isEqualToString:@"blob"])  

  47.     {  

  48.         mtype = ftBlob;  

  49.         return;  

  50.     }  

  51.     else if ([tp isEqualToString:@"date"])  

  52.     {  

  53.         mtype = ftDate;  

  54.     }  

  55.     else if ([tp isEqualToString:@"time"])  

  56.     {  

  57.         mtype = ftTime;  

  58.     }  

  59.     else if ([tp isEqualToString:@"timestamp"])  

  60.     {  

  61.         mtype = ftDateTime;  

  62.     }  

  63.     else if ([tp isEqualToString:@"binary"])  

  64.     {  

  65.         mtype = ftBinary;  

  66.         return;  

  67.     }  

  68.     char* floatstr = (char*)sqlite3_value_text(value);  

  69.     if (floatstr) {  

  70.         result = [NSString stringWithCString:floatstr encoding:NSUTF8StringEncoding];  

  71.     }  

  72.     fieldValue = result;  

  73. }  

  74. -(NSString*)toString  

  75. {  

  76.     return (NSString*)fieldValue;  

  77. }  

  78. -(NSInteger)toInteger  

  79. {  

  80.     return (int)fieldValue;  

  81. }  

  82. -(NSNumber*)toNumber  

  83. {  

  84.     switch (mtype) {  

  85.         case ftFloat:  

  86.             return [NSNumber numberWithFloat:[(NSString*)fieldValue floatValue]];  

  87.             break;  

  88.         case ftDouble:  

  89.             return [NSNumber numberWithDouble:[(NSString*)fieldValue doubleValue]];   

  90.             break;  

  91.         case ftBool:  

  92.             return [NSNumber numberWithBool:[(NSString*)fieldValue boolValue]];  

  93.             break;  

  94.         default:  

  95.             return [NSNumber numberWithInt:[(NSString*)fieldValue intValue]];  

  96.             break;  

  97.     }  

  98. }  

  99. -(NSString*)toDateString  

  100. {  

  101.     NSDateFormatter* fmt = [[[NSDateFormatter alloc]init]autorelease];  

  102.     [fmt setDateFormat:@"yyyy-mm-dd"];  

  103.     NSDate* date = [fmt dateFromString:fieldValue];  

  104.     NSString* datestr = [fmt stringFromDate:date];  

  105.     return (datestr==nil) ? @"":datestr;  

  106. }  

  107. -(NSString*)toTimeString  

  108. {  

  109.     NSDateFormatter* fmt = [[[NSDateFormatter alloc]init]autorelease];  

  110.     [fmt setDateFormat:@"HH:mm:ss"];//H為0-23,h為1-12  

  111.     NSDate* time = [fmt dateFromString:fieldValue];  

  112.     NSString* timestr = [fmt stringFromDate:time];  

  113.     return (timestr==nil) ? @"":timestr;  

  114. }  

  115. -(NSString*)toDateTimeString;  

  116. {  

  117.     NSDateFormatter* fmt = [[[NSDateFormatter alloc]init]autorelease];  

  118.     [fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//H為0-23,h為1-12  

  119.     NSDate* datetime = [fmt dateFromString:fieldValue];  

  120.     NSString* dtimestr = [fmt stringFromDate:datetime];  

  121.     return (dtimestr==nil) ? @"":dtimestr;  

  122. }  

  123. -(NSDate*)toDate  

  124. {  

  125.      NSDateFormatter* fmt = [[NSDateFormatter alloc]init];  

  126.      [fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];  

  127.      NSDate* date = [fmt dateFromString:fieldValue];  

  128.      return date;  

  129. }  

  130. -(enum fieldtype)getFieldType  

  131. {  

  132.     return mtype;  

  133. }  

  134. @end  

  135. @implementation OCDataset  

  136. -(id)init  

  137. {  

  138.     self = [super init];  

  139.     if (self) {  

  140.         cursor = -1;  

  141.         records = [[NSMutableArray alloc]init];  

  142.         return self;  

  143.     }  

  144.     return nil;  

  145. }  

  146. -(void)dealloc  

  147. {  

  148.     [records release];  

  149.     [super dealloc];  

  150. }  

  151. -(void)reset  

  152. {  

  153.     cursor = 0;  

  154. }  

  155. -(void)fillData:(sqlite3_stmt*) cmd  

  156. {  

  157.     NSInteger colcount = sqlite3_column_count(cmd);  

  158.     NSMutableDictionary* fields = [[[NSMutableDictionary alloc]init]autorelease];  

  159.     for (NSInteger i = 0; i < colcount; i++) {  

  160.         char* fieldname = (char*)sqlite3_column_name(cmd, i);  

  161.         NSString* strfieldname = [NSString stringWithCString:fieldname encoding:NSUTF8StringEncoding];  

  162.         sqlite3_value* mvalue = sqlite3_column_value(cmd, i);  

  163.         char* ity = (char*)sqlite3_column_decltype(cmd, i);  

  164.         NSString* stype = [NSString stringWithCString:ity encoding:NSUTF8StringEncoding];  

  165.         //int type = sqlite3_column_type(cmd, i);  

  166.         OCField* field = [[OCField alloc]init];  

  167.         [field setfield:strfieldname withvalue:mvalue withtype:stype];  

  168.         field.seq_column = i;  

  169.         [fields setObject:field forKey:strfieldname];  

  170.     }  

  171.     [records addObject:fields];  

  172. }  

  173. -(void)clear  

  174. {  

  175.     [records removeAllObjects];  

  176.     cursor = -1;  

  177. }  

  178. -(NSInteger)count  

  179. {  

  180.     return [records count];  

  181. }  

  182. -(OCField*)fieldbyname:(NSString*) fieldname  

  183. {  

  184.     NSMutableDictionary* dic = [records objectAtIndex:cursor];  

  185.     return (OCField*)[dic objectForKey:fieldname];  

  186. }  

  187. -(BOOL)next  

  188. {  

  189.     ++cursor;  

  190.     int rcount = [records count];  

  191.     if (cursor <= rcount) {  

  192.         return YES;  

  193.     }  

  194.     else  

  195.     {  

  196.         cursor = rcount+1;  

  197.         return NO;  

  198.     }  

  199. }  

  200. -(BOOL)first  

  201. {  

  202.     if ([records count]>0) {  

  203.         cursor = 0;  

  204.         return YES;  

  205.     }  

  206.     else  

  207.     {  

  208.         cursor = -1;  

  209.         return NO;  

  210.     }  

  211. }  

  212. -(BOOL)move:(NSInteger) index  

  213. {  

  214.     int idx = index -1;  

  215.     if (-1 < idx < [records count]) {  

  216.         cursor = idx;  

  217.         return YES;  

  218.     }  

  219.     return NO;  

  220. }  

  221. -(OCField*)indexOffield:(NSInteger) index  

  222. {  

  223.     OCField* ret = nil;  

  224.     int ct = 0;  

  225.     NSMutableDictionary* dic = [records objectAtIndex:cursor];  

  226.     for (NSString* key in dic)  

  227.     {  

  228.         ret = [dic objectForKey:key];  

  229.         if (index == ct)  

  230.             break;  

  231.         else  

  232.             ct++;  

  233.     }  

  234.     return ret;  

  235. }  

  236. @end  

  237. @implementation OCSqlite  

  238. @synthesize dataset;  

  239. -(id)init  

  240. {  

  241.     self = [super init];  

  242.     if (self) {  

  243.         dataset = [[OCDataset alloc]init];  

  244.         return self;  

  245.     }  

  246.     return nil;  

  247. }  

  248. -(void)dealloc  

  249. {  

  250.     [dataset release];  

  251.     sqlite3_close(db);  

  252.     [super dealloc];  

  253. }  

  254. -(BOOL)ConnectToDB:(NSString*) dbfilepath  

  255. {  

  256.     BOOL successful;  

  257.     successful = sqlite3_open([dbfilepath UTF8String], &db)==SQLITE_OK;  

  258.     if (!successful) {  

  259.         sqlite3_close(db);  

  260.         return NO;  

  261.     }  

  262.     return YES;  

  263. }  

  264. -(void)DisconnectDB  

  265. {  

  266.     sqlite3_close(db);   

  267. }  

  268. -(BOOL)excesql:(NSString*) ddlsql  

  269. {  

  270.     char* err;  

  271.     if (sqlite3_exec(db, [ddlsql UTF8String], NULL, NULL, &err)!=SQLITE_OK)  

  272.     {  

  273.         return NO;  

  274.     }  

  275.     return YES;  

  276. }  

  277. -(BOOL)query:(NSString*) qysql  

  278. {  

  279.     sqlite3_stmt* cmd;  

  280.     if (sqlite3_prepare_v2(db, [qysql UTF8String], -1, &cmd, nil)!=SQLITE_OK)  

  281.     {  

  282.         return NO;  

  283.     }  

  284.     [dataset clear];  

  285.     while(sqlite3_step(cmd)==SQLITE_ROW)  

  286.     {  

  287.         [dataset fillData:cmd];  

  288.     }  

  289.     [dataset reset];  

  290.     sqlite3_finalize(cmd);  

  291.     return YES;  

  292. }  

  293. -(BOOL)startTranslation  

  294. {  

  295.     char* err;  

  296.     if (sqlite3_exec(db, "begin transaction",NULL, NULL, &err)!=SQLITE_OK)  

  297.     {  

  298.         return NO;  

  299.     }  

  300.     return YES;  

  301. }  

  302. -(BOOL)commitTranslation  

  303. {  

  304.     char* err;  

  305.     if (sqlite3_exec(db, "commit transaction",NULL, NULL, &err)!=SQLITE_OK)  

  306.     {  

  307.         return NO;  

  308.     }  

  309.     return YES;  

  310. }  

  311. -(BOOL)rollbackTranslation  

  312. {  

  313.     char* err;  

  314.     if (sqlite3_exec(db, "rollback transaction",NULL, NULL, &err)!=SQLITE_OK)  

  315.     {  

  316.         return NO;  

  317.     }  

  318.     return YES;  

  319. }  

  320. @end 

以上是“sqlite操作與封裝的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文名稱:sqlite操作與封裝的示例分析
文章源于:http://jinyejixie.com/article18/ggiggp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、虛擬主機(jī)、小程序開發(fā)軟件開發(fā)、商城網(wǎng)站ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司