- 论坛徽章:
- 0
|
Objective-C SQLiteHelper
如果你要用这些代码,请无比仔细测试。
欢迎各种拍砖、探讨!
你可以从这里下载- 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 #import <Foundation/Foundation.h> typedef enum _db_op_result{ SUCCESSED = 0, FAILED = 1, CREATE_TABLE_FAILED = 5, TRANSACTION_EXE_FAILED = 7, UPDATE_FAILED = 9, DELETE_FAILED = 10, NOT_ALL_DONE = 20} SMPDB_OPERATION_RESULT; typedef enum _db_op_type{ UPDATE = 1, DELETE = 2, INSERT = 3,} SMPDB_OPERTION_TYPE; @protocol DbOperationDelegate <NSObject> @optional- (NSInteger)udpate;- (NSInteger)insert;- (NSInteger)delete;- (NSArray *)select:(NSString *)condition;- (NSString *)tableName;- (NSString *)fieldsString;- (NSString *)queryString; @end // SQLite Operation#import <Foundation/Foundation.h>#import "sqlite3.h"#import "DbOperationDelegate.h" @interface SMPDbUtil : NSObject{ NSString *databasePath; sqlite3 *db;} @property(readonly)sqlite3 *db; - (NSInteger)createDatabase:(NSString *)dbName;- (NSInteger)insert:(id<DbOperationDelegate>)obj;- (NSInteger)insertSet:(NSString *)tableName tableFields:(NSArray *)fields valueSets:(NSArray *)values;- (sqlite3_stmt *)select:(id<DbOperationDelegate>)obj conditions:(NSString *)cons;- (NSInteger)update:(id<DbOperationDelegate>)obj; @end //how to use#import <UIKit/UIKit.h>#import "DbOperationDelegate.h" @class ProvinceInfo; @interface CityInfo : NSObject<DbOperationDelegate>{ NSInteger cityId; NSString *cityName; ProvinceInfo *province;} @property(assign)NSInteger cityId;@property(copy)NSString *cityName;@property(retain)ProvinceInfo *province; - (NSInteger)udpate;- (NSInteger)insert;- (NSInteger)delete;- (NSArray *)select;- (NSString *)fieldsString;- (NSString *)queryString; @end //.m#import "CityInfo.h"#import "ProvinceInfo.h"#import "SMPDbUtil.h" #define TABLE_NAME @"city" @implementation CityInfo @synthesize cityId, cityName, province; - (NSString *)tableName{ return @"city";} - (NSString *)fieldsString{ return @" id, name, provinceId ";} - (NSString *)queryString{ return [NSString stringWithFormat:@"%d, \"%@\", %d", self.cityId, self.cityName, ((self.province == nil? -1 : self.province.provinceId))];} - (NSInteger)udpate{ SMPDbUtil *dbUtil = [[SMPDbUtil alloc]init]; NSInteger r = [dbUtil update:self]; [dbUtil release]; return r;} - (NSInteger)insert{ SMPDbUtil *dbUtil = [[SMPDbUtil alloc]init]; NSInteger r = [dbUtil insert:self]; [dbUtil release]; return r;} //- (NSInteger)delete{// return 0;//} - (NSArray *)select:(NSString *)condition{ SMPDbUtil *dbUtil = [[SMPDbUtil alloc]init]; sqlite3_stmt *statement = [dbUtil select:self conditions:condition]; if (statement == NULL) { sqlite3_close(dbUtil.db); return nil; } NSMutableArray *array = [[[NSMutableArray alloc]init]autorelease]; while (sqlite3_step(statement) == SQLITE_ROW) { CityInfo *el = [[CityInfo alloc]init]; NSInteger id = sqlite3_column_int(statement, 0); NSString *name = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)]; NSInteger provinceId = sqlite3_column_int(statement, 2); ProvinceInfo *p = [[ProvinceInfo alloc]init]; NSArray *proList = [p select:[NSString stringWithFormat:@"id = %d", provinceId]]; if (proList != nil && proList.count > 0) { p = [proList objectAtIndex:0]; } el.cityId = id; el.cityName = name; el.province = p; [array addObject:el]; [el release]; } sqlite3_close(dbUtil.db); [dbUtil release]; return array;} @end
复制代码 |
|