DBManager.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // DB_Helper.m
  3. // LNManager
  4. //
  5. // Created by EchoShacolee on 2017/4/12.
  6. // Copyright © 2017年 lee. All rights reserved.
  7. //
  8. #import "DBManager.h"
  9. #import <FMDB/FMDB.h>
  10. @implementation DBManager
  11. {
  12. //数据库对象
  13. FMDatabase *_database;
  14. }
  15. /**
  16. 创建数据库类的单例对象
  17. **/
  18. + (DBManager *)sharedManager {
  19. static DBManager *manager = nil;
  20. @synchronized(self) {//同步 执行 防止多线程操作
  21. if (manager == nil) {
  22. manager = [[DBManager alloc] init];
  23. }
  24. }
  25. return manager;
  26. }
  27. - (id)init {
  28. if (self = [super init]) {
  29. //1.获取数据库文件app.db的路径
  30. NSString *filePath = [self getFileFullPathWithFileName:SQLITE_NAME];
  31. //2.创建database
  32. _database = [[FMDatabase alloc] initWithPath:filePath];
  33. //3.open
  34. //第一次 数据库文件如果不存在那么 会创建并且打开
  35. //如果存在 那么直接打开
  36. if ([_database open]) {
  37. // NSLog(@"数据库打开成功");
  38. }else {
  39. NSLog(@"database open failed:%@",_database.lastErrorMessage);
  40. }
  41. }
  42. return self;
  43. }
  44. #pragma mark - 获取文件的全路径
  45. //获取文件在沙盒中的 Documents中的路径
  46. - (NSString *)getFileFullPathWithFileName:(NSString *)fileName {
  47. NSString * bundlePath = [[NSBundle mainBundle]pathForResource:fileName ofType:@""];
  48. NSFileManager *fm = [NSFileManager defaultManager];
  49. if ([fm fileExistsAtPath:bundlePath]) {
  50. // NSLog(@"%@",bundlePath);
  51. return bundlePath;
  52. }else {
  53. //如果不存在可以创建一个新的
  54. NSLog(@"Documents不存在");
  55. return nil;
  56. }
  57. }
  58. -(NSArray*)getAreaFromSqlite {
  59. NSMutableArray *questions=[[NSMutableArray alloc] init];
  60. if ([_database open]) {
  61. NSString *sql = @"select * from area order by parent_code";
  62. FMResultSet *s=[_database executeQuery:sql];
  63. while ([s next]) {
  64. NSMutableDictionary *question = [NSMutableDictionary dictionary];
  65. question[@"name"] = [s stringForColumn:@"name"];
  66. question[@"code"] = [s stringForColumn:@"code"];
  67. question[@"parent_code"] = [s stringForColumn:@"parent_code"];
  68. [questions addObject:question];
  69. }
  70. }else{
  71. NSLog(@"ERROR: db unOpen");
  72. }
  73. NSMutableArray *firArr = [NSMutableArray array];//市
  74. NSMutableArray *secArr = [NSMutableArray array];//区
  75. NSMutableArray *first_Codes = [NSMutableArray array];
  76. for (int i = 0; i < questions.count;i++) {
  77. NSDictionary* area = questions[i];
  78. NSInteger pLen= [area[@"parent_code"] length];
  79. if (pLen == 2) {
  80. [firArr addObject:area];
  81. [first_Codes addObject:area[@"code"]];
  82. [secArr addObject:[NSMutableArray array]];
  83. }else if(pLen >= 4){
  84. NSMutableArray *arr = secArr[[first_Codes indexOfObject:area[@"parent_code"]]];
  85. [arr addObject:area];
  86. }
  87. }
  88. //最后两行数据 漳州开发区,平潭开发区 没什么意义
  89. [firArr removeLastObject];
  90. [firArr removeLastObject];
  91. [secArr removeLastObject];
  92. [secArr removeLastObject];
  93. if(![_database close]) {
  94. NSLog(@"数据库关闭异常,请检查");;
  95. }
  96. return @[firArr,secArr];
  97. }
  98. -(NSMutableArray *)loadArea
  99. {
  100. NSMutableArray *questions=[[NSMutableArray alloc] init];
  101. if ([_database open]) {
  102. NSString *sql = @"select * from area order by parent_code";//
  103. FMResultSet *s=[_database executeQuery:sql];
  104. while ([s next]) {
  105. NSMutableDictionary* question = [NSMutableDictionary dictionary];
  106. question[@"name"] = [s stringForColumn:@"name"];
  107. question[@"code"] = [s stringForColumn:@"code"];
  108. question[@"parent_code"] = [s stringForColumn:@"parent_code"];
  109. [questions addObject:question];
  110. }
  111. }else{
  112. NSLog(@"ERROR: db unOpen");
  113. }
  114. NSMutableArray *firArr = [NSMutableArray array];
  115. NSMutableArray *secArr = [NSMutableArray array];
  116. NSMutableArray* areaArr = questions;
  117. NSMutableArray *first_Codes = [NSMutableArray array];
  118. for (int i = 0; i < areaArr.count;i++) {
  119. NSDictionary* area = areaArr[i];
  120. NSInteger pLen= [area[@"parent_code"] length];
  121. if (pLen == 2) {
  122. [firArr addObject:area[@"name"]];
  123. [first_Codes addObject:area[@"code"]];
  124. [secArr addObject:[NSMutableArray array]];
  125. }else if(pLen >= 4){
  126. NSMutableArray *arr = secArr[[first_Codes indexOfObject:area[@"parent_code"]]];
  127. [arr addObject:area[@"name"]];
  128. }
  129. }
  130. //最后两行数据 漳州开发区,平潭开发区 没什么意义
  131. [firArr removeLastObject];
  132. [firArr removeLastObject];
  133. [secArr removeLastObject];
  134. [secArr removeLastObject];
  135. //返回内容 @【市名数组,对应区名数组,all_dic数组】
  136. return [NSMutableArray arrayWithObjects:firArr,secArr,areaArr, nil];
  137. }
  138. @end