Objective-C SQLiteHelper
为毛要用CoreData,SQLite不是挺好吗?
如果你要用这些代码,请无比仔细测试。
如果你要用这些代码,请无比仔细测试。
欢迎各种拍砖、探讨!
你可以从这里下载
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: 572064792 | Nodejs:329118122
做人要厚道,转载请注明出处!