@@ -52,34 +52,38 @@ -(void)test
52
52
{
53
53
addText (@" 示例 开始 example start \n\n " );
54
54
55
- // 清空数据库
55
+ // /获取 LKTest 类使用的 LKDBHelper
56
56
LKDBHelper* globalHelper = [LKTest getUsingLKDBHelper ];
57
57
58
+ // /删除所有表 delete all table
58
59
[globalHelper dropAllTable ];
59
60
60
61
addText (@" LKTest create table sql :\n %@ \n " ,[LKTest getCreateTableSQL ]);
61
62
addText (@" LKTestForeign create table sql :\n %@ \n " ,[LKTestForeign getCreateTableSQL ]);
62
63
63
- // 清空表数据 clear table data
64
+ // 清空表数据 clear table data
64
65
[LKDBHelper clearTableData: [LKTest class ]];
65
66
67
+ // 初始化数据模型 init object
68
+ LKTest* test = [[LKTest alloc ]init];
69
+ test.name = @" zhan san" ;
70
+ test.age = 16 ;
66
71
72
+ // 外键 foreign key
67
73
LKTestForeign* foreign = [[LKTestForeign alloc ]init];
68
74
foreign.address = @" :asdasdasdsadasdsdas" ;
69
75
foreign.postcode = 123341 ;
70
76
foreign.addid = 213214 ;
71
77
72
- // 插入数据 insert table row
73
- LKTest* test = [[LKTest alloc ]init];
74
- test.name = @" zhan san" ;
75
- test.age = 16 ;
76
-
77
- // 外键 foreign key
78
78
test.address = foreign;
79
+
80
+
81
+ // /复杂对象 complex object
79
82
test.blah = @[@" 1" ,@" 2" ,@" 3" ];
80
83
test.blah = @[@" 0" ,@[@1 ],@{@" 2" :@2 },foreign];
81
84
test.hoho = @{@" array" :test.blah ,@" foreign" :foreign,@" normal" :@123456 ,@" date" :[NSDate date ]};
82
85
86
+ // /other
83
87
test.isGirl = YES ;
84
88
test.like = ' I' ;
85
89
test.img = [UIImage imageNamed: @" 41.png" ];
@@ -92,80 +96,85 @@ -(void)test
92
96
test.data = [@" hahaha" dataUsingEncoding: NSUTF8StringEncoding];
93
97
94
98
addText (@" %f " ,test.score );
95
- // 异步 插入第一条 数据 Insert the first
99
+ // 同步 插入第一条 数据 synchronous insert the first
96
100
[test saveToDB ];
97
101
// or
98
102
// [globalHelper insertToDB:test];
99
103
100
- // 多主键 的插入成功
104
+ // 更改主键继续插入 Insert the change after the primary key
101
105
test.age = 17 ;
102
106
[globalHelper insertToDB: test];
103
107
104
108
// 事物 transaction
105
- [globalHelper executeDB: ^(FMDatabase *db) {
106
-
107
- [db beginTransaction ];
109
+ [globalHelper executeForTransaction: ^BOOL (LKDBHelper *helper) {
108
110
109
111
test.name = @" 1" ;
110
- [globalHelper insertToDB: test];
112
+ BOOL success = [helper insertToDB: test];
111
113
112
114
test.name = @" 2" ;
113
- [globalHelper insertToDB: test];
115
+ success = [helper insertToDB: test];
114
116
115
117
// 重复主键 duplicate primary key
116
118
test.name = @" 1" ;
117
119
test.rowid = 0 ; // no new object,should set rowid:0
118
- BOOL insertSucceed = [globalHelper insertWhenNotExists: test];
119
-
120
+ BOOL insertSucceed = [helper insertWhenNotExists: test];
121
+
120
122
// insert fail
121
123
if (insertSucceed == NO )
122
- [db rollback ];
124
+ {
125
+ // /rollback
126
+ return NO ;
127
+ }
123
128
else
124
- [db commit ];
125
-
129
+ {
130
+ // /commit
131
+ return YES ;
132
+ }
126
133
}];
127
134
128
135
129
136
addText (@" 同步插入 完成! Insert completed synchronization" );
130
137
131
138
sleep (1 );
132
139
133
-
134
- // 改个 主键 插入第2条数据 update primary column value Insert the second
135
140
test.name = @" li si" ;
136
141
[globalHelper insertToDB: test callback: ^(BOOL isInsert) {
137
142
addText (@" asynchronization insert complete: %@ " ,isInsert>0 ?@" YES" :@" NO" );
138
143
}];
139
144
140
145
// 查询 search
141
- addText (@" 同步搜索 sync search" );
142
-
143
- NSMutableArray * arraySync = nil ;
144
- arraySync = [globalHelper searchWithSQL: @" select * from @t" toClass: [LKTest class ]];
145
- for (id obj in arraySync) {
146
+ NSMutableArray * searchResultArray = nil ;
147
+
148
+ addText (@" \n search one: \n " );
149
+ // /同步搜索 执行sql语句 把结果变为LKTest对象
150
+ // /Synchronous search executes the SQL statement put the results into a LKTest object
151
+ searchResultArray = [globalHelper searchWithSQL: @" select * from @t" toClass: [LKTest class ]];
152
+ for (id obj in searchResultArray) {
146
153
addText (@" %@ " ,[obj printAllPropertys ]);
147
154
}
148
155
149
- // 查询 search
150
- addText ( @" 同步搜索 sync search 2 " );
151
- arraySync = [LKTest searchWithWhere: nil orderBy: nil offset: 0 count: 100 ];
152
- for (id obj in arraySync ) {
156
+ addText ( @" \n search two: \n " );
157
+ // /搜索所有值 search all
158
+ searchResultArray = [LKTest searchWithWhere: nil orderBy: nil offset: 0 count: 100 ];
159
+ for (id obj in searchResultArray ) {
153
160
addText (@" %@ " ,[obj printAllPropertys ]);
154
161
}
155
162
156
- // 查询 单个 列 search single column
157
- addText ( @" 只获取name那列的值 search with column 'name' results" );
163
+ addText ( @" 查询 单个 列 search single column" );
164
+ // / 只获取name那列的值 search with column 'name' results
158
165
NSArray * nameArray = [LKTest searchColumn: @" name" where: nil orderBy: nil offset: 0 count: 0 ];
159
166
addText (@" %@ " ,[nameArray componentsJoinedByString: @" ," ]);
160
167
168
+
169
+ // /
161
170
addText (@" 休息2秒 开始 为了说明 是异步插入的\n "
162
171
" rest for 2 seconds to start is asynchronous inserted to illustrate" );
163
172
164
173
sleep (2 );
165
174
166
175
addText (@" 休息2秒 结束 \n rest for 2 seconds at the end" );
167
176
168
- // 异步
177
+ // 异步 asynchronous
169
178
[globalHelper search: [LKTest class ] where: nil orderBy: nil offset: 0 count: 100 callback: ^(NSMutableArray *array) {
170
179
171
180
addText (@" 异步搜索 结束, async search end" );
@@ -175,21 +184,23 @@ -(void)test
175
184
176
185
sleep (1 );
177
186
178
- // 修改 update
187
+ // / 修改 update object
179
188
LKTest* test2 = [array objectAtIndex: 0 ];
180
189
test2.name = @" wang wu" ;
181
190
182
191
[globalHelper updateToDB: test2 where: nil ];
183
192
184
193
addText (@" 修改完成 , update completed " );
185
194
195
+ // /all
186
196
array = [globalHelper search: [LKTest class ] where: nil orderBy: nil offset: 0 count: 100 ];
187
197
for (NSObject * obj in array) {
188
198
addText (@" %@ " ,[obj printAllPropertys ]);
189
199
}
190
200
191
- test2.rowid = 0 ;
192
201
202
+ // /delete
203
+ test2.rowid = 0 ;
193
204
BOOL ishas = [globalHelper isExistsModel: test2];
194
205
if (ishas)
195
206
{
@@ -200,6 +211,7 @@ -(void)test
200
211
addText (@" 删除完成, delete completed" );
201
212
sleep (1 );
202
213
214
+ // /all
203
215
array = [globalHelper search: [LKTest class ] where: nil orderBy: nil offset: 0 count: 100 ];
204
216
for (NSObject * obj in array) {
205
217
addText (@" %@ " ,[obj printAllPropertys ]);
0 commit comments