如果想更深的理解sqlITE的封请学习FMDB,FMDB是对sqlITE的线程访问封装。
以下是本人以个人习惯简单的封装。
符合D开发的 *** 作者习惯。
ocsqlite.h
//// OCsqlite.m// sqlite//// Created by fengsh on 12-12-3.// copyright (c) 2012年 sqlite_lib. All rights reserved.///* 对SQLITE的封装,主要设计是 *** 作上的习惯。对于轻量级数据量可以。 对于在数据量就需要注意内存的开销了。 */#import <Foundation/Foundation.h>#import <sqlite3.h>enum fIEldtype{ ftInt,ftfloat,ftDouble,ftString,ftBlob,ftBool,ftDate,ftTime,ftDateTime,ftBinary};/* 字段类 作用:主要用于与数据库中的字段属性进行对应 字段名,字段类型,字段值,字段索引号 */@interface OCFIEld : NSObject { Nsstring* fIEldname; ID fIEldValue; enum fIEldtype mtype; int seq_column;}-(Nsstring*)toString;-(NSInteger)toInteger;-(NSDate*)toDate;-(Nsstring*)toDateString;-(Nsstring*)toTimeString;-(Nsstring*)toDateTimeString;-(NSNumber*)toNumber;-(enum fIEldtype)getFIEldType;@property (nonatomic) int seq_column;@end/* 数据集类 作用: 类似于数据源的集合,带游标,可访问数据源中的数据 */@interface Ocdataset : NSObject{ NSMutableArray* records; NSInteger cursor;}-(voID)clear;-(NSInteger)count;-(BOol)next;-(BOol)first;-(BOol)move:(NSInteger) index;-(OCFIEld*)fIEldbyname:(Nsstring*) fIEldname;-(OCFIEld*)indexOffIEld:(NSInteger) index;@end/*简单的数据定义语言 *** 作及数据库查询的封装 未支持参数绑定,因此在处理blob上还需要扩展代码。 后续完善 */@interface OCsqlite : 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//// Created by fengsh on 12-12-3.// copyright (c) 2012年 sqlite_lib. All rights reserved.//#import "OCsqlite.h"@implementation OCFIEld@synthesize seq_column;-(ID)init{ self = [super init]; if (self) { fIEldValue = NulL; return self; } return nil;}-(voID)setfIEld:(Nsstring*) name withvalue:(sqlite3_value*) value withtype:(Nsstring*) tp{ fIEldname = name; Nsstring* result = @""; if ([tp isEqualToString:@"integer"]||[tp isEqualToString:@"smallint"]) { mtype = ftInt; fIEldValue = (ID)sqlite3_value_int(value); return; } else if ([tp isEqualToString:@"boolean"]) { mtype = ftBool; } else if ([tp isEqualToString:@"float"]) { mtype = ftfloat; } else if ([tp isEqualToString:@"double"]||[tp isEqualToString:@"real"]) { mtype = ftDouble; } else if ([tp isEqualToString:@"text"]||[tp isEqualToString:@"varchar"]) { mtype = ftString; } else if ([tp isEqualToString:@"blob"]) { mtype = ftBlob; return; } else if ([tp isEqualToString:@"date"]) { mtype = ftDate; } else if ([tp isEqualToString:@"time"]) { mtype = ftTime; } else if ([tp isEqualToString:@"timestamp"]) { mtype = ftDateTime; } else if ([tp isEqualToString:@"binary"]) { mtype = ftBinary; return; } char* floatstr = (char*)sqlite3_value_text(value); if (floatstr) { result = [Nsstring stringWithCString:floatstr enCoding:NSUTF8StringEnCoding]; } fIEldValue = result;}-(Nsstring*)toString{ return (Nsstring*)fIEldValue;}-(NSInteger)toInteger{ return (int)fIEldValue;}-(NSNumber*)toNumber{ switch (mtype) { case ftfloat: return [NSNumber numberWithfloat:[(Nsstring*)fIEldValue floatValue]]; break; case ftDouble: return [NSNumber numberWithDouble:[(Nsstring*)fIEldValue doubleValue]]; break; case ftBool: return [NSNumber numberWithBool:[(Nsstring*)fIEldValue boolValue]]; break; default: return [NSNumber numberWithInt:[(Nsstring*)fIEldValue intValue]]; break; } }-(Nsstring*)toDateString{ NSDateFormatter* fmt = [[[NSDateFormatter alloc]init]autorelease]; [fmt setDateFormat:@"yyyy-mm-dd"]; NSDate* date = [fmt dateFromString:fIEldValue]; Nsstring* datestr = [fmt stringFromDate:date]; return (datestr==nil) ? @"":datestr;}-(Nsstring*)toTimeString{ NSDateFormatter* fmt = [[[NSDateFormatter alloc]init]autorelease]; [fmt setDateFormat:@"HH:mm:ss"];//H为0-23,h为1-12 NSDate* time = [fmt dateFromString:fIEldValue]; Nsstring* timestr = [fmt stringFromDate:time]; return (timestr==nil) ? @"":timestr;}-(Nsstring*)toDateTimeString;{ NSDateFormatter* fmt = [[[NSDateFormatter alloc]init]autorelease]; [fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//H为0-23,h为1-12 NSDate* datetime = [fmt dateFromString:fIEldValue]; Nsstring* dtimestr = [fmt stringFromDate:datetime]; return (dtimestr==nil) ? @"":dtimestr;}-(NSDate*)toDate{ NSDateFormatter* fmt = [[NSDateFormatter alloc]init]; [fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate* date = [fmt dateFromString:fIEldValue]; return date; }-(enum fIEldtype)getFIEldType{ return mtype;}@end@implementation Ocdataset-(ID)init{ self = [super init]; if (self) { cursor = -1; records = [[NSMutableArray alloc]init]; return self; } return nil;}-(voID)dealloc{ [records release]; [super dealloc];}-(voID)reset{ cursor = 0;}-(voID)fillData:(sqlite3_stmt*) cmd{ NSInteger colcount = sqlite3_column_count(cmd); NSMutableDictionary* fIElds = [[[NSMutableDictionary alloc]init]autorelease]; for (NSInteger i = 0; i < colcount; i++) { char* fIEldname = (char*)sqlite3_column_name(cmd,i); Nsstring* strfIEldname = [Nsstring stringWithCString:fIEldname enCoding:NSUTF8StringEnCoding]; sqlite3_value* mvalue = sqlite3_column_value(cmd,i); char* ity = (char*)sqlite3_column_decltype(cmd,i); Nsstring* stype = [Nsstring stringWithCString:ity enCoding:NSUTF8StringEnCoding]; //int type = sqlite3_column_type(cmd,i); OCFIEld* fIEld = [[OCFIEld alloc]init]; [fIEld setfIEld:strfIEldname withvalue:mvalue withtype:stype]; fIEld.seq_column = i; [fIElds setobject:fIEld forKey:strfIEldname]; } [records addobject:fIElds];}-(voID)clear{ [records removeAllObjects]; cursor = -1;}-(NSInteger)count{ return [records count];}-(OCFIEld*)fIEldbyname:(Nsstring*) fIEldname{ NSMutableDictionary* dic = [records objectAtIndex:cursor]; return (OCFIEld*)[dic objectForKey:fIEldname];}-(BOol)next{ ++cursor; int rcount = [records count]; if (cursor <= rcount) { return YES; } else { cursor = rcount+1; return NO; }}-(BOol)first{ if ([records count]>0) { cursor = 0; return YES; } else { cursor = -1; return NO; }}-(BOol)move:(NSInteger) index{ int IDx = index -1; if (-1 < IDx < [records count]) { cursor = IDx; return YES; } return NO;}-(OCFIEld*)indexOffIEld:(NSInteger) index{ OCFIEld* ret = nil; int ct = 0; NSMutableDictionary* dic = [records objectAtIndex:cursor]; for (Nsstring* key in dic) { ret = [dic objectForKey:key]; if (index == ct) break; else ct++; } return ret;}@end@implementation OCsqlite@synthesize dataset;-(ID)init{ self = [super init]; if (self) { dataset = [[Ocdataset alloc]init]; return self; } return nil;}-(voID)dealloc{ [dataset release]; sqlite3_close(db); [super dealloc];}-(BOol)ConnectToDB:(Nsstring*) dbfilepath{ BOol successful; successful = sqlite3_open([dbfilepath UTF8String],&db)==sqlITE_OK; if (!successful) { sqlite3_close(db); return NO; } return YES;}-(voID)disconnectDB{ sqlite3_close(db); }-(BOol)excesql:(Nsstring*) ddlsql{ char* err; if (sqlite3_exec(db,[ddlsql UTF8String],NulL,&err)!=sqlITE_OK) { return NO; } return YES;}-(BOol)query:(Nsstring*) qysql{ sqlite3_stmt* cmd; if (sqlite3_prepare_v2(db,[qysql UTF8String],-1,&cmd,nil)!=sqlITE_OK) { return NO; } [dataset clear]; while(sqlite3_step(cmd)==sqlITE_ROW) { [dataset fillData:cmd]; } [dataset reset]; sqlite3_finalize(cmd); return YES;}-(BOol)startTranslation{ char* err; if (sqlite3_exec(db,"begin transaction",&err)!=sqlITE_OK) { return NO; } return YES;}-(BOol)commitTranslation{ char* err; if (sqlite3_exec(db,"commit transaction",&err)!=sqlITE_OK) { return NO; } return YES;}-(BOol)rollbackTranslation{ char* err; if (sqlite3_exec(db,"rollback transaction",&err)!=sqlITE_OK) { return NO; } return YES;}@end总结
以上是内存溢出为你收集整理的sqlite *** 作与封装全部内容,希望文章能够帮你解决sqlite *** 作与封装所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)