Objective-C SQLiteHelper

简介:

你可以从这里下载 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#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

 

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2012/03/02/2377647.html
相关文章
|
5月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
302 2
|
3月前
|
开发工具 iOS开发 容器
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错
iOS Objective-C 应用连接Azure Storage时,若不关闭账号的匿名访问,程序能正常运行。但关闭匿名访问后,上传到容器时会出现错误:“Public access is not permitted”。解决方法是将创建容器时的公共访问类型从`AZSContainerPublicAccessTypeContainer`改为`AZSContainerPublicAccessTypeOff`,以确保通过授权请求访问。
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错
|
5月前
|
缓存 开发工具 iOS开发
优化iOS中Objective-C代码调起支付流程的速度
优化iOS中Objective-C代码调起支付流程的速度
62 2
|
5月前
|
安全 JavaScript 前端开发
IOS开发基础知识:介绍一下 Swift 和 Objective-C,它们之间有什么区别?
IOS开发基础知识:介绍一下 Swift 和 Objective-C,它们之间有什么区别?
192 0
|
iOS开发 容器
iOS 代码规范格式 Objective-C(上)
iOS 代码规范格式 Objective-C
422 0
iOS 代码规范格式 Objective-C(上)
|
编译器 API iOS开发
iOS 代码规范格式 Objective-C(下)
iOS 代码规范格式 Objective-C(下)
221 0
|
机器学习/深度学习 API iOS开发
【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词(一)
【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词(一)
169 0
|
存储 自然语言处理 Java
【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词(二)
【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词(二)
241 0
|
存储 安全 C语言
【iOS 开发】Objective - C 面向对象 - 方法 | 成员变量 | 隐藏封装 | KVC | KVO | 初始化 | 多态(一)
【iOS 开发】Objective - C 面向对象 - 方法 | 成员变量 | 隐藏封装 | KVC | KVO | 初始化 | 多态(一)
189 0