项目地址

Division

查询方法

1
2
3
4
5
6
7
--SELECT * from org_division WHERE parent_id='root' ORDER BY create_time ASC;

--查询市
--SELECT * from org_division WHERE parent_id='20c49eb015cf48e9a45eee413578e62f';

--查询区
--SELECT * from org_division WHERE parent_id='13afafd6a30f4169a11a6ef8174b6581';

案例

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
NSString *path = [[NSBundle mainBundle] pathForResource:@"4330d398bfc148e2964492aead40b907" ofType:@"sqlite3"];
FMDatabase *fmdb = [FMDatabase databaseWithPath:path];

if ([fmdb open]) {
fmdb.logsErrors = YES;
//1.query
NSString *query = [NSString stringWithFormat:@"SELECT * from org_division WHERE parent_id='%@' ORDER BY create_time ASC",parentId == nil ? @"root" : parentId];
FMResultSet *rs = [fmdb executeQuery:query];
// 2.遍历结果集
NSMutableArray *arrM = [NSMutableArray array];
while (rs.next) {
NSMutableDictionary* dictM = [[NSMutableDictionary alloc] init];
for (int i=0;i<[[[rs columnNameToIndexMap] allKeys] count];i++) {
dictM[[rs columnNameForIndex:i]] = [rs objectForColumnIndex:i];
}
GPAdministrativeDivisionsModel *m = [GPAdministrativeDivisionsModel new];
m.divisionCode = dictM[@"division_code"];
m.divisionName = dictM[@"division_name"];
m.divisionId = dictM[@"division_id"];
m.parentId = dictM[@"parent_id"];
m.divisionType = @"";
//单独处理4个直辖市
if ([m.divisionName isEqualToString:@"北京市"] || [m.divisionName isEqualToString:@"上海市"]
|| [m.divisionName isEqualToString:@"天津市"] || [m.divisionName isEqualToString:@"重庆市"]) {
m.divisionType = @"Municipality";
}
[arrM addObject:m];
}
//查询完后要关闭rs,不然会报@"Warning: there is at least one open result set around after performing
[rs close];
[fmdb close];
if (completion) {
completion(arrM,nil);
}
}else{
NSLog(@"db is not open.");
if (completion) {
completion(nil,[NSError errorWithDomain:NSURLErrorDomain code:@"400" userInfo:@{NSLocalizedDescriptionKey:@"内部数据库打开失败"}]);
}
}