免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1447 | 回复: 1
打印 上一主题 下一主题

Objective-C SQLiteHelper [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-04 16:53 |只看该作者 |倒序浏览
Objective-C SQLiteHelper





如果你要用这些代码,请无比仔细测试。

欢迎各种拍砖、探讨!



你可以从这里下载
  1. 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
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-04 16:54 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP