如果想更深的理解sqlITE的封请学习FMDB,FMDB是对sqlITE的线程访问封装。
以下是本人以个人习惯简单的封装。
符合D开发的操作者习惯。
ocsqlite.h
- //
- //OCsqlite.m
- //sqlite
- //
- //Createdbyfengshon12-12-3.
- //Copyright(c)2012年sqlite_Lib.Allrightsreserved.
- //
- /*
- 对SQLITE的封装,主要设计是操作上的习惯。对于轻量级数据量可以。
- 对于在数据量就需要注意内存的开销了。
- */
- #import<Foundation/Foundation.h>
- #import<sqlite3.h>
- enumfieldtype
- {
- ftInt,ftFloat,ftDouble,ftString,ftBlob,ftBool,ftDate,ftTime,ftDateTime,ftBinary
- };
- /*
- 字段类
- 作用:主要用于与数据库中的字段属性进行对应
- 字段名,字段类型,字段值,字段索引号
- */
- @interfaceOCField:NSObject
- {
- NSString*fieldName;
- idfieldValue;
- enumfieldtypemtype;
- intseq_column;
- }
- -(NSString*)toString;
- -(NSInteger)toInteger;
- -(NSDate*)toDate;
- -(NSString*)toDateString;
- -(NSString*)toTimeString;
- -(NSString*)toDateTimeString;
- -(NSNumber*)toNumber;
- -(enumfieldtype)getFieldType;
- @property(nonatomic)intseq_column;
- @end
- /*
- 数据集类
- 作用:
- 类似于数据源的集合,带游标,可访问数据源中的数据
- */
- @interfaceOCDataset:NSObject
- {
- NSMutableArray*records;
- NSIntegercursor;
- }
- -(void)clear;
- -(NSInteger)count;
- -(BOOL)next;
- -(BOOL)first;
- -(BOOL)move:(NSInteger)index;
- -(OCField*)fieldbyname:(NSString*)fieldname;
- -(OCField*)indexOffield:(NSInteger)index;
- @end
- /*
- 简单的数据定义语言操作及数据库查询的封装
- 未支持参数绑定,因此在处理blob上还需要扩展代码。
- 后续完善
- */
- @interfaceOCsqlite:NSObject
- {
- sqlite3*db;
- OCDataset*dataset;
- }
- -(id)init;
- -(BOOL)ConnectToDB:(NSString*)dbfilepath;
- -(void)DisconnectDB;
- -(BOOL)startTranslation;
- -(BOOL)commitTranslation;
- -(BOOL)rollbackTranslation;
- -(BOOL)excesql:(NSString*)ddlsql;
- -(BOOL)query:(NSString*)qysql;
- @property(nonatomic,readonly)OCDataset*dataset;
- @end
ocsqlite.m
- //
- //OCsqlite.m
- //sqlite
- //
- //Createdbyfengshon12-12-3.
- //Copyright(c)2012年sqlite_Lib.Allrightsreserved.
- //
- #import"OCsqlite.h"
- @implementationOCField
- @synthesizeseq_column;
- -(id)init
- {
- self=[superinit];
- if(self){
- fieldValue=NULL;
- returnself;
- }
- returnnil;
- }
- -(void)setfield:(NSString*)namewithvalue:(sqlite3_value*)valuewithtype:(NSString*)tp
- {
- fieldName=name;
- NSString*result=@"";
- if([tpisEqualToString:@"integer"]||[tpisEqualToString:@"smallint"])
- {
- mtype=ftInt;
- fieldValue=(id)sqlite3_value_int(value);
- return;
- }
- elseif([tpisEqualToString:@"boolean"])
- {
- mtype=ftBool;
- }
- elseif([tpisEqualToString:@"float"])
- {
- mtype=ftFloat;
- }
- elseif([tpisEqualToString:@"double"]||[tpisEqualToString:@"real"])
- {
- mtype=ftDouble;
- }
- elseif([tpisEqualToString:@"text"]||[tpisEqualToString:@"varchar"])
- {
- mtype=ftString;
- }
- elseif([tpisEqualToString:@"blob"])
- {
- mtype=ftBlob;
- return;
- }
- elseif([tpisEqualToString:@"date"])
- {
- mtype=ftDate;
- }
- elseif([tpisEqualToString:@"time"])
- {
- mtype=ftTime;
- }
- elseif([tpisEqualToString:@"timestamp"])
- {
- mtype=ftDateTime;
- }
- elseif([tpisEqualToString:@"binary"])
- {
- mtype=ftBinary;
- return;
- }
- char*floatstr=(char*)sqlite3_value_text(value);
- if(floatstr){
- result=[NSStringstringWithCString:floatstrencoding:NSUTF8StringEncoding];
- }
- fieldValue=result;
- }
- -(NSString*)toString
- {
- return(NSString*)fieldValue;
- }
- -(NSInteger)toInteger
- {
- return(int)fieldValue;
- }
- -(NSNumber*)toNumber
- {
- switch(mtype){
- caseftFloat:
- return[NSNumbernumberWithFloat:[(NSString*)fieldValuefloatValue]];
- break;
- caseftDouble:
- return[NSNumbernumberWithDouble:[(NSString*)fieldValuedoubleValue]];
- break;
- caseftBool:
- return[NSNumbernumberWithBool:[(NSString*)fieldValueboolValue]];
- break;
- default:
- return[NSNumbernumberWithInt:[(NSString*)fieldValueintValue]];
- break;
- }
- }
- -(NSString*)toDateString
- {
- NSDateFormatter*fmt=[[[NSDateFormatteralloc]init]autorelease];
- [fmtsetDateFormat:@"yyyy-mm-dd"];
- NSDate*date=[fmtdateFromString:fieldValue];
- NSString*datestr=[fmtstringFromDate:date];
- return(datestr==nil)?@"":datestr;
- }
- -(NSString*)toTimeString
- {
- NSDateFormatter*fmt=[[[NSDateFormatteralloc]init]autorelease];
- [fmtsetDateFormat:@"HH:mm:ss"];//H为0-23,h为1-12
- NSDate*time=[fmtdateFromString:fieldValue];
- NSString*timestr=[fmtstringFromDate:time];
- return(timestr==nil)?@"":timestr;
- }
- -(NSString*)toDateTimeString;
- {
- NSDateFormatter*fmt=[[[NSDateFormatteralloc]init]autorelease];
- [fmtsetDateFormat:@"yyyy-MM-ddHH:mm:ss"];//H为0-23,h为1-12
- NSDate*datetime=[fmtdateFromString:fieldValue];
- NSString*dtimestr=[fmtstringFromDate:datetime];
- return(dtimestr==nil)?@"":dtimestr;
- }
- -(NSDate*)toDate
- {
- NSDateFormatter*fmt=[[NSDateFormatteralloc]init];
- [fmtsetDateFormat:@"yyyy-MM-ddHH:mm:ss"];
- NSDate*date=[fmtdateFromString:fieldValue];
- returndate;
- }
- -(enumfieldtype)getFieldType
- {
- returnmtype;
- }
- @end
- @implementationOCDataset
- -(id)init
- {
- self=[superinit];
- if(self){
- cursor=-1;
- records=[[NSMutableArrayalloc]init];
- returnself;
- }
- returnnil;
- }
- -(void)dealloc
- {
- [recordsrelease];
- [superdealloc];
- }
- -(void)reset
- {
- cursor=0;
- }
- -(void)fillData:(sqlite3_stmt*)cmd
- {
- NSIntegercolcount=sqlite3_column_count(cmd);
- NSMutableDictionary*fields=[[[NSMutableDictionaryalloc]init]autorelease];
- for(NSIntegeri=0;i<colcount;i++){
- char*fieldname=(char*)sqlite3_column_name(cmd,i);
- NSString*strfieldname=[NSStringstringWithCString:fieldnameencoding:NSUTF8StringEncoding];
- sqlite3_value*mvalue=sqlite3_column_value(cmd,i);
- char*ity=(char*)sqlite3_column_decltype(cmd,i);
- NSString*stype=[NSStringstringWithCString:ityencoding:NSUTF8StringEncoding];
- //inttype=sqlite3_column_type(cmd,i);
- OCField*field=[[OCFieldalloc]init];
- [fieldsetfield:strfieldnamewithvalue:mvaluewithtype:stype];
- field.seq_column=i;
- [fieldssetObject:fieldforKey:strfieldname];
- }
- [recordsaddObject:fields];
- }
- -(void)clear
- {
- [recordsremoveAllObjects];
- cursor=-1;
- }
- -(NSInteger)count
- {
- return[recordscount];
- }
- -(OCField*)fieldbyname:(NSString*)fieldname
- {
- NSMutableDictionary*dic=[recordsobjectAtIndex:cursor];
- return(OCField*)[dicobjectForKey:fieldname];
- }
- -(BOOL)next
- {
- ++cursor;
- intrcount=[recordscount];
- if(cursor<=rcount){
- returnYES;
- }
- else
- {
- cursor=rcount+1;
- returnNO;
- }
- }
- -(BOOL)first
- {
- if([recordscount]>0){
- cursor=0;
- returnYES;
- }
- else
- {
- cursor=-1;
- returnNO;
- }
- }
- -(BOOL)move:(NSInteger)index
- {
- intidx=index-1;
- if(-1<idx<[recordscount]){
- cursor=idx;
- returnYES;
- }
- returnNO;
- }
- -(OCField*)indexOffield:(NSInteger)index
- {
- OCField*ret=nil;
- intct=0;
- NSMutableDictionary*dic=[recordsobjectAtIndex:cursor];
- for(NSString*keyindic)
- {
- ret=[dicobjectForKey:key];
- if(index==ct)
- break;
- else
- ct++;
- }
- returnret;
- }
- @end
- @implementationOCsqlite
- @synthesizedataset;
- -(id)init
- {
- self=[superinit];
- if(self){
- dataset=[[OCDatasetalloc]init];
- returnself;
- }
- returnnil;
- }
- -(void)dealloc
- {
- [datasetrelease];
- sqlite3_close(db);
- [superdealloc];
- }
- -(BOOL)ConnectToDB:(NSString*)dbfilepath
- {
- BOOLsuccessful;
- successful=sqlite3_open([dbfilepathUTF8String],&db)==sqlITE_OK;
- if(!successful){
- sqlite3_close(db);
- returnNO;
- }
- returnYES;
- }
- -(void)DisconnectDB
- {
- sqlite3_close(db);
- }
- -(BOOL)excesql:(NSString*)ddlsql
- {
- char*err;
- if(sqlite3_exec(db,[ddlsqlUTF8String],NULL,&err)!=sqlITE_OK)
- {
- returnNO;
- }
- returnYES;
- }
- -(BOOL)query:(NSString*)qysql
- {
- sqlite3_stmt*cmd;
- if(sqlite3_prepare_v2(db,[qysqlUTF8String],-1,&cmd,nil)!=sqlITE_OK)
- {
- returnNO;
- }
- [datasetclear];
- while(sqlite3_step(cmd)==sqlITE_ROW)
- {
- [datasetfillData:cmd];
- }
- [datasetreset];
- sqlite3_finalize(cmd);
- returnYES;
- }
- -(BOOL)startTranslation
- {
- char*err;
- if(sqlite3_exec(db,"begintransaction",&err)!=sqlITE_OK)
- {
- returnNO;
- }
- returnYES;
- }
- -(BOOL)commitTranslation
- {
- char*err;
- if(sqlite3_exec(db,"committransaction",&err)!=sqlITE_OK)
- {
- returnNO;
- }
- returnYES;
- }
- -(BOOL)rollbackTranslation
- {
- char*err;
- if(sqlite3_exec(db,"rollbacktransaction",&err)!=sqlITE_OK)
- {
- returnNO;
- }
- returnYES;
- }
- @end