???????????Ч???


?????????????????
#pragma mark -- ???????
//?????????????
- (void)openSqlData{
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory?? NSUserDomainMask?? YES) lastObject];
NSLog(@"sql·??===%@"??doc);
NSString *fileName = [doc stringByAppendingPathComponent:@"city.sqlite"];
FMDatabaseQueue *dbQueue = [FMDatabaseQueue databaseQueueWithPath:fileName];
[dbQueue inDatabase:^(FMDatabase *db) {
BOOL result = [db executeUpdate:@"create table if not exists t_city (id integer primary key autoincrement??pid integer not null??name text not null??pinyin text not null??firstletter text not null??hotcity integer not null);"];
if (result) {
NSLog(@"?????");
}else{
NSLog(@"?????");
}
}];
self.dbQueue = dbQueue;
}
??????????????飬????????в???????
//?????????????????????
- (void)insertData:(CityModel *)cityModel{
DefineWeakSelf;
//????????????
__block NSString *insertStr = @"";
[self.dbQueue inDatabase:^(FMDatabase *db) {
NSString *deleteStr = [NSString stringWithFormat:@"delete from t_city"];
if (![db executeUpdate:deleteStr]) {
NSLog(@"??????????????????");
}
//????????
[db beginTransaction];
BOOL isRollBack = NO;
@try {
[cityModel.positionList enumerateObjectsUsingBlock:^(CityEntity *entity?? NSUInteger idx?? BOOL * _Nonnull stop) {
insertStr =[NSString stringWithFormat:@"insert into t_city(pid??name??pinyin??firstletter??hotcity) values ('%@'??'%@'??'%@'??'%@'??'%@');"??@(entity.pid)??entity.name??entity.pinyin??entity.firstLetter??@(0)];
if (![db executeUpdate:insertStr]) {
NSLog(@"???????????");
}
}];
} @catch (NSException *exception) {
//??????????????
isRollBack = YES;
[db rollback];
} @finally {
//??????
if (!isRollBack) {
[db commit];
}
}
}];
}
???????????????浽?????
//?????????????
- (void)queryData{
DefineWeakSelf;
[self.dbQueue inDatabase:^(FMDatabase *db) {
NSString *firstStr = @"a";
//??в????? ????????????
FMResultSet *resultSet = [db executeQuery:@"select * from t_city order by firstletter asc"];
//???????
while ([resultSet next]) {
NSString *title = [resultSet stringForColumn:@"firstletter"];
NSString *name = [resultSet stringForColumn:@"name"];
NSInteger hotcity = [resultSet intForColumn:@"hotcity"];
if (hotcity == 1) {
[wSelf.hotcityArray addObject:name];
}
if ([title isEqualToString:firstStr]) {
[wSelf.cityArray addObject:name];
}else{
[wSelf.titleArray addObject:[firstStr uppercaseString]];
wSelf.cityDictionary[firstStr] = wSelf.cityArray;
firstStr = title;
wSelf.cityArray = [NSMutableArray array];
[wSelf.cityArray addObject:name];
}
}
if (![firstStr isEqualToString:@"a"]) {
[wSelf.titleArray addObject:[firstStr uppercaseString]];
wSelf.cityDictionary[firstStr] = wSelf.cityArray;
}
}];
[self.cityChooseTableView reloadData];
}
???????????
//??????
- (void)queryByString:(NSString *)queryStr{
DefineWeakSelf;
_isSearch = YES;
_searchArr = [NSMutableArray array];
[self.dbQueue inDatabase:^(FMDatabase *db) {
//??????????????? ???????
NSString *sql = [NSString stringWithFormat:@"select * from t_city where pinyin like '%@%%' or name like '%@%%';"??[queryStr lowercaseString]??queryStr];
FMResultSet *resultSet = [db executeQuery:sql];
while ([resultSet next]) {
NSLog(@"%@"??[resultSet stringForColumn:@"name"]);
[wSelf.searchArr addObject:[resultSet stringForColumn:@"name"]];
}
[wSelf.cityChooseTableView reloadData];
}];
}