DB_Que_Helper.m 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. #import "DB_Que_Helper.h"
  2. #import "Question.h"
  3. #import "markGroup.h"
  4. #import "markInfo.h"
  5. #import "Grade.h"
  6. #import <sqlite3.h>
  7. static FMDatabase *shareDataBase = nil;
  8. @interface DB_Que_Helper ()
  9. @property (copy, readwrite, nonatomic) NSDictionary *updateDataDic;
  10. @property (copy, readwrite, nonatomic) NSArray *updateIdArr;
  11. @end
  12. @implementation DB_Que_Helper
  13. /**
  14. 创建数据库类的单例对象
  15. **/
  16. + (FMDatabase *)createDataBase {
  17. if (![[NSFileManager defaultManager] fileExistsAtPath:dataBasePath2]) {
  18. [DB_Que_Helper copyFileDatabase];
  19. }
  20. /**线程安全的。
  21. 其实就是一个单例的方法
  22. 好处:
  23. 1 线程安全
  24. 2 很好满足静态分析器要求
  25. 3 和自动引用计数(ARC)兼容
  26. 4 仅需要少量代码
  27. 缺点:
  28. 仍可以创建非共享的实例
  29. */
  30. static dispatch_once_t onceToken;
  31. dispatch_once(&onceToken, ^{
  32. shareDataBase = [FMDatabase databaseWithPath:dataBasePath2];
  33. /// 升级函数
  34. FMDBMigrationManager * manager = [FMDBMigrationManager managerWithDatabaseAtPath:dataBasePath2 migrationsBundle:[NSBundle mainBundle]];
  35. BOOL resultState=NO;
  36. NSError * error=nil;
  37. if (!manager.hasMigrationsTable) {
  38. resultState=[manager createMigrationsTable:&error];
  39. }
  40. resultState = [manager migrateDatabaseToVersion:UINT64_MAX progress:nil error:&error];
  41. });
  42. //如果数据库搞定 判断表是否存在
  43. //添加成绩表
  44. [DB_Que_Helper createNewTable];
  45. //添加错题、收藏、排错、已做题表
  46. [DB_Que_Helper createNewQuestionTable];
  47. //添加表后 如果有旧数据 就导入
  48. if ([myDelegate.gradeTableData isKindOfClass:[NSMutableArray class]]) {
  49. if (myDelegate.gradeTableData.count > 0) {
  50. if ([shareDataBase open]) {
  51. for (NSDictionary *dic in myDelegate.gradeTableData) {
  52. [shareDataBase executeUpdate:[NSString stringWithFormat:@"INSERT INTO grade (date,score,time,honor,subject,car_type,sfzhm) VALUES ('%@','%@','%@','%@','%@','%@','%@');", dic[@"data"],dic[@"score"],dic[@"time"],dic[@"honor"],dic[@"subject"],dic[@"car_type"],dic[@"sfzhm"]]];
  53. }
  54. [shareDataBase close];
  55. }
  56. }
  57. [myDelegate.gradeTableData removeAllObjects];
  58. myDelegate.gradeTableData = nil;
  59. }
  60. if ([myDelegate.markQuestionTableData isKindOfClass:[NSMutableArray class]]) {
  61. if (myDelegate.markQuestionTableData.count > 0) {
  62. if ([shareDataBase open]) {
  63. for (NSDictionary *dic in myDelegate.markQuestionTableData) {
  64. [shareDataBase executeUpdate:[NSString stringWithFormat:@"INSERT INTO markQuestion (_id,type,isUpdata,subject,car_type,sfzhm)values(%d,'%@','%@','%@','%@','%@')", [dic[@"_id"] intValue],dic[@"type"],dic[@"isUpdata"],dic[@"subject"],dic[@"car_type"],dic[@"sfzhm"]]];
  65. }
  66. [shareDataBase close];
  67. }
  68. }
  69. [myDelegate.markQuestionTableData removeAllObjects];
  70. myDelegate.markQuestionTableData = nil;
  71. }
  72. if ([shareDataBase open]) {
  73. int num = -1;
  74. FMResultSet *s = [shareDataBase executeQuery:@"SELECT * FROM db_versions WHERE id=1"];
  75. if ([s next]) {
  76. num=[[s stringForColumn:@"version"] intValue];
  77. if (num == 0) {
  78. for (NSString *idStr in self.updateIdArr) {
  79. [self updateQuestionWithId:idStr];
  80. }
  81. NSString *sqlStr = [NSString stringWithFormat:@"UPDATE db_versions SET version = 1 WHERE id=1"];
  82. BOOL isSuccess = [shareDataBase executeUpdate:sqlStr];
  83. NSLog(@"%@",isSuccess? @"Success!" : @"Failed!");
  84. }
  85. }
  86. [shareDataBase close];
  87. }
  88. return shareDataBase;
  89. }
  90. /**创建grade表。保存我的成绩
  91. */
  92. +(void)createNewTable
  93. {
  94. if ([shareDataBase open])
  95. {
  96. //创建表 如果表不存在
  97. [shareDataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS grade (id integer PRIMARY KEY AUTOINCREMENT, date text, score text, time text , honor text,subject text ,car_type text ,sfzhm text);"];
  98. //判断字段是否存在 老学员的表中没有这个字段 这里判断添加 并赋初始值
  99. NSString *sfzhm = @"sfzhm";
  100. if (![shareDataBase columnExists:sfzhm inTableWithName:@"grade"])
  101. {
  102. NSString *sql;
  103. sql = [NSString stringWithFormat:@"ALTER TABLE grade ADD sfzhm text"];
  104. [shareDataBase executeUpdate:sql];
  105. [DB_Que_Helper setGradeDefaultValue];
  106. }else{
  107. //NSLog(@"sfzhm已存在");
  108. }
  109. [shareDataBase close];
  110. }
  111. }
  112. +(void)createNewQuestionTable
  113. {
  114. if ([shareDataBase open])
  115. {
  116. //创建错题类表 type:1、错题 2、收藏 3、排错 4、已做未做 isUpdata:是否上传
  117. [shareDataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS markQuestion (id integer PRIMARY KEY AUTOINCREMENT, _id integer, type text, isUpdata text, subject text ,car_type text ,sfzhm text);"];
  118. [shareDataBase close];
  119. }
  120. }
  121. +(void)insertGrade:(Grade*)gra
  122. {
  123. if ([shareDataBase open])
  124. {
  125. //将考试成绩存到数据库
  126. NSString *sfzhm = @"no";
  127. if (myDelegate.isLogin) {
  128. sfzhm = RQ_USER_MANAGER.currentUser._id;
  129. }
  130. [shareDataBase executeUpdate:@"INSERT INTO grade (date,score,time,honor,subject,car_type,sfzhm) VALUES (?,?,?,?,?,?,?);", gra.graDate,gra.graScore,gra.graTime,gra.graHonor,myDelegate.subject,RQ_YDTQuestion_Module.car_type,sfzhm];
  131. [shareDataBase close];
  132. }
  133. }
  134. /**注意:表也是有题库名和科目的 按照题库和科目 找到对应的成绩
  135. */
  136. +(NSArray *)queryAllGrades
  137. {
  138. NSMutableArray *arr=[[NSMutableArray alloc]init];
  139. shareDataBase=[DB_Que_Helper createDataBase];
  140. if ([shareDataBase open]) {
  141. NSString *sfzhm = @"no";
  142. if (myDelegate.isLogin) {
  143. sfzhm = RQ_USER_MANAGER.currentUser._id;
  144. }
  145. FMResultSet *s=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from grade where car_type = '%@' and subject = '%@' and sfzhm = '%@'",RQ_YDTQuestion_Module.car_type,myDelegate.subject,sfzhm]];
  146. while ([s next]) {
  147. Grade *gra=[[Grade alloc]init];
  148. gra.graDate=[s stringForColumn:@"date"];
  149. gra.graHonor=[s stringForColumn:@"honor"];
  150. gra.graScore=[s stringForColumn:@"score"];
  151. gra.graTime=[s stringForColumn:@"time"];
  152. [arr addObject:gra];
  153. //NSLog(@"邪门---><>%@--->%@----.%@----->%@",gra.graDate,gra.graHonor,gra.graScore,gra.graTime);
  154. }
  155. }else{
  156. // NSLog(@"成绩数据库未打开----db unOpen");
  157. [LoadingView showMsg:@"读取成绩失败!"];
  158. }
  159. return arr;
  160. }
  161. ///**添加done和favorite字段
  162. // 现在用的 iscollect 和 isdone 字段 这个方法不用了吧 不过如果需要添加字段 可以使用这个方法
  163. // 可以删除 --danson
  164. // */
  165. //+(void)addDone{
  166. // //判断字段是否存在
  167. // NSString* done = @"done";
  168. // [shareDataBase open];
  169. // if (![shareDataBase columnExists:done inTableWithName:@"question"])
  170. // {
  171. // NSString *sql;
  172. // sql = [NSString stringWithFormat:@"ALTER TABLE question ADD done text"];
  173. // [shareDataBase executeUpdate:sql];
  174. // [DB_Que_Helper setDefaultValue];
  175. // }else{
  176. // //NSLog(@"done已存在");
  177. // }
  178. //
  179. // [shareDataBase close];
  180. //}
  181. +(void)addDoneAndFavorite{
  182. //判断字段是否存在
  183. NSString* done = @"done";
  184. [shareDataBase open];
  185. if (![shareDataBase columnExists:done inTableWithName:@"question"])
  186. {
  187. NSString *sql;
  188. sql = [NSString stringWithFormat:@"ALTER TABLE question ADD done text"];
  189. [shareDataBase executeUpdate:sql];
  190. sql = [NSString stringWithFormat:@"ALTER TABLE question ADD favorite text"];
  191. [shareDataBase executeUpdate:sql];
  192. [DB_Que_Helper setDefaultValue];
  193. }else{
  194. // NSLog(@"done&fav exist");
  195. }
  196. [shareDataBase close];
  197. }
  198. +(BOOL)setStateWithArray:(NSArray *)questions
  199. {
  200. BOOL isOk = NO;
  201. BOOL allIsOK = YES;
  202. shareDataBase=[DB_Que_Helper createDataBase];
  203. if ([shareDataBase open]) {
  204. for (NSDictionary* obj in questions) {
  205. NSInteger qId = [obj[@"QID"] integerValue];
  206. NSString *kType = obj[@"TYPE"];
  207. NSString *sql;
  208. if ([kType isEqualToString:@"1"])
  209. {
  210. //收藏
  211. sql = [NSString stringWithFormat:@"update question set iscollect = %@ where _id=%ld",@"1",(long)qId];
  212. }
  213. else if ([kType isEqualToString:@"2"])
  214. {
  215. //错题
  216. sql = [NSString stringWithFormat:@"update question set state = %@ where _id=%ld",@"1",(long)qId];
  217. }
  218. else
  219. {
  220. //排除
  221. sql = [NSString stringWithFormat:@"update question set isdone = %@ where _id=%ld",@"1",(long)qId];
  222. }
  223. isOk=[shareDataBase executeUpdate:sql];
  224. if (isOk == NO) {
  225. allIsOK = NO;
  226. }
  227. }
  228. //同步数据后 查询错题数量
  229. NSString *sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and state = '1' and subject = '1'",RQ_YDTQuestion_Module.car_type];
  230. int cnt1 = [shareDataBase intForQuery:sql];
  231. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and state = '1' and subject = '4'",RQ_YDTQuestion_Module.car_type];
  232. int cnt2 = [shareDataBase intForQuery:sql];
  233. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and state = '1'",RQ_YDTQuestion_Module.car_type];
  234. int cnt3 = [shareDataBase intForQuery:sql];
  235. myDelegate.faultArray = [NSMutableArray arrayWithObjects:[NSString stringWithFormat:@"%d",cnt1],[NSString stringWithFormat:@"%d",cnt2],[NSString stringWithFormat:@"%d",cnt3], nil];
  236. [shareDataBase close];
  237. }
  238. //NSLog(@"数据库数据更新%@",allIsOK == 1 ? @"成功" : @"失败");
  239. //NSLog(@"错题书------>%@",myDelegate.faultArray);
  240. return allIsOK;
  241. }
  242. //如果同步失败 或无数据做的操作
  243. +(void)getFaultIfNotNet
  244. {
  245. shareDataBase=[DB_Que_Helper createDataBase];
  246. if ([shareDataBase open]) {
  247. NSString *sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and state = 1 and subject = 1",RQ_YDTQuestion_Module.car_type];
  248. int cnt1 = [shareDataBase intForQuery:sql];
  249. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and state = 1 and subject = 4",RQ_YDTQuestion_Module.car_type];
  250. int cnt2 = [shareDataBase intForQuery:sql];
  251. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and state = 1",RQ_YDTQuestion_Module.car_type];
  252. int cnt3 = [shareDataBase intForQuery:sql];
  253. myDelegate.faultArray = [NSMutableArray arrayWithObjects:[NSString stringWithFormat:@"%d",cnt1],[NSString stringWithFormat:@"%d",cnt2],[NSString stringWithFormat:@"%d",cnt3], nil];
  254. [shareDataBase close];
  255. }
  256. }
  257. +(BOOL)setState:(NSString*)state withId:(NSInteger)qid withType:(NSString *)type
  258. {
  259. NSString *sql;
  260. if ([type isEqualToString:@"1"])
  261. {
  262. //收藏
  263. sql = [NSString stringWithFormat:@"update question set iscollect = %@ where _id=%ld",state,(long)qid];
  264. }
  265. else if ([type isEqualToString:@"2"])
  266. {
  267. //错题
  268. sql = [NSString stringWithFormat:@"update question set state = %@ where _id=%ld",state,(long)qid];
  269. }
  270. else
  271. {
  272. //排除
  273. // NSLog(@"数据库更改排错状态");
  274. sql = [NSString stringWithFormat:@"update question set isdone = %@ where _id=%ld",state,(long)qid];
  275. }
  276. BOOL isOk = NO;
  277. shareDataBase=[DB_Que_Helper createDataBase];
  278. if ([shareDataBase open]) {
  279. isOk=[shareDataBase executeUpdate:sql];
  280. [shareDataBase close];
  281. }
  282. //NSLog(@"数据库数据更新%@",isOk == 1 ? @"成功" : @"失败");
  283. return isOk;
  284. }
  285. //如果新加了字段 要有这个操作置零 --danson
  286. +(void)setDefaultValue
  287. {
  288. /**fmdb一定要重新开。因为刚才没有2个字段。现在有了2个字段。
  289. */
  290. static dispatch_once_t onceToken;
  291. dispatch_once(&onceToken, ^{
  292. shareDataBase = [FMDatabase databaseWithPath:dataBasePath2];
  293. });
  294. if ([shareDataBase open]) {
  295. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update question set done = 0"]];
  296. }
  297. [shareDataBase close];
  298. }
  299. +(void)setGradeDefaultValue
  300. {
  301. /**fmdb一定要重新开。因为刚才没有2个字段。现在有了2个字段。
  302. */
  303. //这样会进死循环
  304. //shareDataBase=[DB_Que_Helper createDataBase];
  305. //这个会导致打开多个数据库
  306. //shareDataBase = [FMDatabase databaseWithPath:dataBasePath2];
  307. //dansonmark
  308. static dispatch_once_t onceToken;
  309. dispatch_once(&onceToken, ^{
  310. shareDataBase = [FMDatabase databaseWithPath:dataBasePath2];
  311. });
  312. if ([shareDataBase open]) {
  313. //ALTER TABLE grade ADD sfzhm text
  314. NSString *sfzhm = @"no";
  315. if (myDelegate.isLogin) {
  316. sfzhm = RQ_USER_MANAGER.currentUser._id;
  317. }
  318. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update grade set sfzhm = '%@'",sfzhm]];
  319. //NSLog(@"写入----><>%d",isok);
  320. }
  321. [shareDataBase close];
  322. }
  323. /**不要判断是否存在,直接写入覆盖。
  324. */
  325. +(void)removeDB
  326. {
  327. NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask, YES) firstObject];
  328. // NSLog(@"移除旧题库----><>%@",documentsDirectory);
  329. NSString *documentLibraryFolderPath = [documentsDirectory stringByAppendingPathComponent:dataBaseName2];
  330. [[NSFileManager defaultManager] removeItemAtPath:documentLibraryFolderPath error:nil];
  331. //重新copy题库
  332. shareDataBase=[DB_Que_Helper createDataBase];
  333. }
  334. + (void)getDataFormLastDB {
  335. if (![[NSFileManager defaultManager] fileExistsAtPath:dataBasePath2]) {
  336. //说明是第一次安装 没有题库 不需要做转移数据的操作
  337. return;
  338. }
  339. // NSLog(@"获取旧题库数据");
  340. shareDataBase=[DB_Que_Helper createDataBase];
  341. if ([shareDataBase open]) {
  342. //提取markQuestion数据
  343. FMResultSet *s=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from markQuestion"]];
  344. myDelegate.markQuestionTableData = [NSMutableArray array];
  345. while ([s next]) {
  346. //id integer PRIMARY KEY AUTOINCREMENT, _id integer, type text, isUpdata text, subject text ,car_type text ,sfzhm text
  347. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  348. [dic setValue:[NSString stringWithFormat:@"%d",[s intForColumn:@"_id"]] forKey:@"_id"];
  349. [dic setValue:[s stringForColumn:@"type"] forKey:@"type"];
  350. [dic setValue:[s stringForColumn:@"isUpdata"] forKey:@"isUpdata"];
  351. [dic setValue:[s stringForColumn:@"subject"] forKey:@"subject"];
  352. [dic setValue:[s stringForColumn:@"car_type"] forKey:@"car_type"];
  353. [dic setValue:[s stringForColumn:@"sfzhm"] forKey:@"sfzhm"];
  354. [myDelegate.markQuestionTableData addObject:dic];
  355. }
  356. //提取grade数据
  357. s=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from grade"]];
  358. myDelegate.gradeTableData = [NSMutableArray array];
  359. while ([s next]) {
  360. //date text, score text, time text , honor text,subject text ,car_type text ,sfzhm text
  361. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  362. [dic setValue:[s stringForColumn:@"text"] forKey:@"text"];
  363. [dic setValue:[s stringForColumn:@"score"] forKey:@"score"];
  364. [dic setValue:[s stringForColumn:@"time"] forKey:@"time"];
  365. [dic setValue:[s stringForColumn:@"honor"] forKey:@"honor"];
  366. [dic setValue:[s stringForColumn:@"subject"] forKey:@"subject"];
  367. [dic setValue:[s stringForColumn:@"car_type"] forKey:@"car_type"];
  368. [dic setValue:[s stringForColumn:@"sfzhm"] forKey:@"sfzhm"];
  369. [myDelegate.gradeTableData addObject:dic];
  370. }
  371. // NSLog(@"getDataFormLastDB---->%@-----%@",myDelegate.markQuestionTableData,myDelegate.gradeTableData);
  372. [shareDataBase close];
  373. }else{
  374. NSLog(@"题库获取失败,数据库未打开_getDataFormLastDB");
  375. }
  376. //提取完数据 删除旧库
  377. [DB_Que_Helper removeDB];
  378. }
  379. /**
  380. **关闭数据库
  381. **/
  382. + (void)closeDataBase {
  383. if(![shareDataBase close]) {
  384. NSLog(@"数据库关闭异常,请检查");
  385. return;
  386. }
  387. }
  388. //拷贝数据库
  389. +(void)copyFileDatabase {
  390. // NSLog(@"拷贝数据库");
  391. NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask, YES);
  392. NSString *documentsDirectory = [paths objectAtIndex:0];
  393. NSString *documentLibraryFolderPath = [documentsDirectory stringByAppendingPathComponent:dataBaseName2];
  394. if ([[NSFileManager defaultManager] fileExistsAtPath:documentLibraryFolderPath]) {
  395. // NSLog(@"222文件已经存在了");
  396. }else {
  397. NSString *dbName = @"miaxis.db";
  398. NSString *resourceSampleImagesFolderPath =[[NSBundle mainBundle] pathForResource:dbName ofType:nil];
  399. NSData *mainBundleFile = [NSData dataWithContentsOfFile:resourceSampleImagesFolderPath];
  400. [[NSFileManager defaultManager] createFileAtPath:documentLibraryFolderPath
  401. contents:mainBundleFile
  402. attributes:nil];
  403. }
  404. }
  405. +(NSArray *)queryAllQuestion
  406. {
  407. NSMutableArray *questions=[[NSMutableArray alloc]init];
  408. shareDataBase=[DB_Que_Helper createDataBase];
  409. if ([shareDataBase open]) {
  410. FMResultSet *s=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from question where car_type = '%@' ",RQ_YDTQuestion_Module.car_type]];
  411. while ([s next]) {
  412. Question *question=[[Question alloc]init];
  413. question._id=[s stringForColumn:@"_id"];
  414. question.option_type=[s stringForColumn:@"option_type"];
  415. question.media_type=[s stringForColumn:@"media_type"];
  416. question.chapter=[s stringForColumn:@"chapter"];
  417. question.questionLabel=[s stringForColumn:@"question_label"];
  418. question.question=[s stringForColumn:@"question"];
  419. question.media=[s stringForColumn:@"media"];
  420. question.answer=[s stringForColumn:@"answer"];
  421. question.optionA=[s stringForColumn:@"option_a"];
  422. question.optionB=[s stringForColumn:@"option_b"];
  423. question.optionC=[s stringForColumn:@"option_c"];
  424. question.optionD=[s stringForColumn:@"option_d"];
  425. // question.explain=[s stringForColumn:@"explain"];
  426. question.explain=[s stringForColumn:@"best_explain_new"];//2023-08-15 ning 替换db-新字段
  427. question.state=[s stringForColumn:@"state"];
  428. question.subject=[s stringForColumn:@"subject"];
  429. [questions addObject:question];
  430. }
  431. [shareDataBase close];
  432. }else{
  433. NSLog(@"题库获取失败,数据库未打开");
  434. }
  435. return questions;
  436. }
  437. +(NSArray*)queryArea
  438. {
  439. /**从数据库找地址信息
  440. */
  441. NSMutableArray *questions=[[NSMutableArray alloc] init];
  442. shareDataBase=[DB_Que_Helper createDataBase];
  443. if ([shareDataBase open]) {
  444. FMResultSet *s=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from area"]];
  445. while ([s next]) {
  446. NSMutableDictionary* question = [NSMutableDictionary dictionary];
  447. question[@"name"] = [s stringForColumn:@"name"];
  448. question[@"code"] = [s stringForColumn:@"code"];
  449. question[@"parent_code"] = [s stringForColumn:@"parent_code"];
  450. [questions addObject:question];
  451. }
  452. [shareDataBase close];
  453. }else{
  454. NSLog(@"ERROR: db unOpen");
  455. }
  456. return questions;
  457. }
  458. +(NSArray *)queryQuestion:(NSString *)where
  459. {
  460. NSMutableArray *questions=[[NSMutableArray alloc]init];
  461. shareDataBase=[DB_Que_Helper createDataBase];
  462. if ([shareDataBase open]) {
  463. NSString *sql;
  464. //章节考试用到
  465. if ([where rangeOfString:@"subject = '1'"].location != NSNotFound)
  466. {
  467. sql=[NSString stringWithFormat:@"select * from question where car_type = '%@' and %@",RQ_YDTQuestion_Module.car_type,where];
  468. }
  469. if ([where rangeOfString:@"subject = '4'"].location != NSNotFound)
  470. {
  471. sql=[NSString stringWithFormat:@"select * from question where car_type = 'C1' and %@",where ];
  472. if ([RQ_YDTQuestion_Module.car_type isEqualToString:@"D"]) {
  473. sql=[NSString stringWithFormat:@"select * from question where car_type = 'D' and %@",where ];
  474. }
  475. }
  476. //资格证 非强化 非全部题
  477. if ([RQ_YDTQuestion_Module.car_type isEqualToString:@"1"] || [RQ_YDTQuestion_Module.car_type isEqualToString:@"2"] || [RQ_YDTQuestion_Module.car_type isEqualToString:@"3"] || [RQ_YDTQuestion_Module.car_type isEqualToString:@"4"] || [RQ_YDTQuestion_Module.car_type isEqualToString:@"5"])
  478. {
  479. if (![where isEqualToString:@"strong"])
  480. {
  481. if (where.length > 12) {
  482. where = [where substringFromIndex:13];
  483. }
  484. sql=[NSString stringWithFormat:@"select * from question where car_type = '%@'%@",RQ_YDTQuestion_Module.car_type,where];
  485. }
  486. }
  487. if ([where isEqualToString:@"strong"])//代表强化练习
  488. {
  489. sql=[NSString stringWithFormat:@"select * from question where %@",myDelegate.where];
  490. }
  491. FMResultSet *s=[shareDataBase executeQuery:sql];
  492. questions = [Question questionsWithFMResultSet:s];
  493. //NSLog(@"这里找出要显示的题目sql-->%@-------where-->%@------count-->%d",sql,where,(int)questions.count);
  494. [shareDataBase close];
  495. }
  496. return questions;
  497. }
  498. //查找章节列表
  499. +(NSArray *)queryList:(NSString *)where
  500. {
  501. NSMutableArray *lists=[[NSMutableArray alloc]init];
  502. shareDataBase=[DB_Que_Helper createDataBase];
  503. if ([shareDataBase open]) {
  504. FMResultSet *s=[shareDataBase executeQuery:where];
  505. while ([s next])
  506. {
  507. NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:[s stringForColumn:@"title"],@"title",[s stringForColumn:@"ID"],@"chapter", nil];
  508. [lists addObject:dic];
  509. }
  510. [shareDataBase close];
  511. }
  512. return lists;
  513. }
  514. //查找标识
  515. +(NSArray *)queryMark:(NSString *)where
  516. {
  517. NSMutableArray *markArray = [NSMutableArray array];
  518. shareDataBase=[DB_Que_Helper createDataBase];
  519. if ([shareDataBase open]) {
  520. FMResultSet *s=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from APP_MARK_GROUP where MG_PARENT = '%@'",where]];
  521. markArray = [markGroup markGroupWithFMResultSet:s];
  522. [shareDataBase close];
  523. }
  524. return markArray;
  525. }
  526. +(NSArray *)queryDetailMark:(NSString *)where
  527. {
  528. NSMutableArray *markArray = [NSMutableArray array];
  529. shareDataBase=[DB_Que_Helper createDataBase];
  530. if ([shareDataBase open]) {
  531. FMResultSet *s=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from APP_MARK_INFO where %@",where]];
  532. markArray = [markInfo markInfoWithFMResultSet:s];
  533. [shareDataBase close];
  534. }
  535. return markArray;
  536. }
  537. +(BOOL)updateDB:(NSString *)sql
  538. {
  539. shareDataBase=[DB_Que_Helper createDataBase];
  540. BOOL isOk = NO;
  541. if ([shareDataBase open])
  542. {
  543. isOk=[shareDataBase executeUpdate:sql];
  544. [shareDataBase close];
  545. }
  546. return isOk;
  547. }
  548. //添加已做的题
  549. +(void)addToDoneQue:(NSInteger)ID
  550. {
  551. shareDataBase=[DB_Que_Helper createDataBase];
  552. if ([shareDataBase open]) {
  553. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update question set done = 1 where _id=%d",(int)ID]];
  554. [shareDataBase close];
  555. }
  556. }
  557. //这里也错了 和数据库字段不符 delete --数据库问题--danson
  558. +(BOOL)addToStrengthenQue:(NSInteger)ID
  559. {
  560. BOOL isOk = NO;
  561. shareDataBase=[DB_Que_Helper createDataBase];
  562. if ([shareDataBase open]) {
  563. isOk=[shareDataBase executeUpdate:[NSString stringWithFormat:@"update question set state = 1 where _id=%d",(int)ID]];
  564. [shareDataBase close];
  565. }
  566. return isOk;
  567. }
  568. +(BOOL)removeToStrengthenQue:(NSInteger)ID
  569. {
  570. BOOL isOk = NO;
  571. shareDataBase=[DB_Que_Helper createDataBase];
  572. if ([shareDataBase open]) {
  573. isOk=[shareDataBase executeUpdate:[NSString stringWithFormat:@"update question set state = 0 where _id=%d",(int)ID]];
  574. [shareDataBase close];
  575. }
  576. return isOk;
  577. }
  578. /**
  579. 抽考试题
  580. */
  581. +(NSArray *)queryExamQuestion:(NSString *)where
  582. {
  583. NSMutableArray *questions=[[NSMutableArray alloc]init];
  584. shareDataBase=[DB_Que_Helper createDataBase];
  585. if ([shareDataBase open]) {
  586. NSString *sql;
  587. //模拟考试中....
  588. //从数据库中随机抽取相应数目的题目 应该不用考虑题目存放数组位置问题吧 因为不是耗时操作 按代码先后顺序来排序
  589. //资格证考试和科一科四不同 40单选 40判断 10多选
  590. //科目一40题判断题 科目四20题判断题
  591. if ([where isEqualToString:@"subject = '1'"])
  592. {
  593. sql=[NSString stringWithFormat:@"select * from question where car_type = '%@' and subject = 1 and option_type = 0 order by random() limit %@",RQ_YDTQuestion_Module.car_type, [RQ_YDTQuestion_Module.car_type isEqualToString:@"D"]? @"20" : @"40"];
  594. }
  595. else if ([where isEqualToString:@"subject = '4'"])
  596. {
  597. sql=[NSString stringWithFormat:@"select * from question where car_type = 'C1' and subject = 4 and option_type = 0 order by random() limit 20"];
  598. }
  599. else
  600. {
  601. sql=[NSString stringWithFormat:@"select * from question where car_type = '%@' and option_type = 1 order by random() limit 40",RQ_YDTQuestion_Module.car_type];
  602. }
  603. FMResultSet *s=[shareDataBase executeQuery:sql];
  604. [questions addObjectsFromArray:[Question questionsWithFMResultSet:s]];
  605. //科目一60题单选题 科目四20题单选题
  606. if ([where isEqualToString:@"subject = '1'"])
  607. {
  608. sql=[NSString stringWithFormat:@"select * from question where car_type = '%@' and subject = 1 and option_type = 1 order by random() limit %@",RQ_YDTQuestion_Module.car_type,[RQ_YDTQuestion_Module.car_type isEqualToString:@"D"]? @"30" : @"60"];
  609. }
  610. else if ([where isEqualToString:@"subject = '4'"])
  611. {
  612. sql=[NSString stringWithFormat:@"select * from question where car_type = 'C1' and subject = 4 and option_type = 1 order by random() limit 20"];
  613. }
  614. else
  615. {
  616. sql=[NSString stringWithFormat:@"select * from question where car_type = '%@' and option_type = 0 order by random() limit 40",RQ_YDTQuestion_Module.car_type];
  617. }
  618. FMResultSet *s2=[shareDataBase executeQuery:sql];
  619. [questions addObjectsFromArray:[Question questionsWithFMResultSet:s2]];
  620. // 科目四 10题多选题(科目一无多选题)
  621. if ([where isEqualToString:@"subject = '4'"])
  622. {
  623. FMResultSet *s3=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from question where car_type = 'C1' and subject = 4 and option_type = 2 order by random() limit 10"]];
  624. [questions addObjectsFromArray:[Question questionsWithFMResultSet:s3]];
  625. }
  626. if ([where isEqualToString:@"subject = nil"])
  627. {
  628. //NSLog(@"资格证多选题");
  629. FMResultSet *s3=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from question where car_type = '%@' and option_type = 2 order by random() limit 10",RQ_YDTQuestion_Module.car_type]];
  630. [questions addObjectsFromArray:[Question questionsWithFMResultSet:s3]];
  631. }
  632. [shareDataBase close];
  633. }
  634. return questions;
  635. }
  636. //清空错题、收藏、排错
  637. +(void)clearFavInfo
  638. {
  639. //每次进程序都要有这样的操作
  640. shareDataBase=[DB_Que_Helper createDataBase];
  641. if([shareDataBase open])
  642. {
  643. [shareDataBase executeUpdate:@"update question set state = 0 where state = 1"];
  644. [shareDataBase executeUpdate:@"update question set state = 0 where iscollect = 1"];
  645. [shareDataBase executeUpdate:@"update question set state = 0 where isdone = 1"];
  646. [shareDataBase close];
  647. return;
  648. }else{
  649. NSLog(@"db error");
  650. return;
  651. }
  652. }
  653. +(BOOL)executeSql:(NSArray *)sqlList
  654. {
  655. shareDataBase=[DB_Que_Helper createDataBase];
  656. if([shareDataBase open]){
  657. NSInteger count=sqlList.count-1;
  658. for(int i=0;i<count;i++){
  659. [shareDataBase executeUpdate:[sqlList objectAtIndex:i]];
  660. }
  661. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update db_versions set version = %@ where id=1",[sqlList objectAtIndex:count]]];
  662. [shareDataBase close];
  663. return YES;
  664. }else{
  665. return NO;
  666. }
  667. }
  668. /**
  669. 从选择页面过来查询多少题目
  670. */
  671. +(NSInteger)countForQuery:(NSString *)where
  672. {
  673. shareDataBase=[DB_Que_Helper createDataBase];
  674. if ([shareDataBase open]) {
  675. NSString *sql;
  676. if ([where isEqualToString:@"subject = 1"])
  677. {
  678. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and %@",RQ_YDTQuestion_Module.car_type,where];
  679. }
  680. else if ([where isEqualToString:@"subject = 4"])
  681. {
  682. //NSLog(@"摩托车科是--%@",RQ_YDTQuestion_Module.car_type);
  683. if ([RQ_YDTQuestion_Module.car_type isEqualToString:@"D"]) {
  684. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = 'D' and %@",where];
  685. }else{
  686. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = 'C1' and %@",where];
  687. }
  688. }
  689. else if ([where isEqualToString:@"nil"])
  690. {
  691. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@'",RQ_YDTQuestion_Module.car_type];
  692. }
  693. else if ([where isEqualToString:@"state = 1 and subject = 1"])
  694. {
  695. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and %@",RQ_YDTQuestion_Module.car_type,where];
  696. }
  697. else if ([where isEqualToString:@"state = 1 and subject = 4"])
  698. {
  699. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = 'C1' and %@",where ];
  700. }
  701. else
  702. {
  703. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and state = 1",RQ_YDTQuestion_Module.car_type];
  704. }
  705. if ([where rangeOfString:@"strong"].location != NSNotFound)
  706. {
  707. sql=[NSString stringWithFormat:@"select count(*) from question where %@",myDelegate.where];
  708. }
  709. // if ([where isEqualToString:@"done"])
  710. // {
  711. // sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and done = 1 and %@",RQ_YDTQuestion_Module.car_type,myDelegate.where];
  712. // if ([myDelegate.where isEqualToString:@"done"])
  713. // {
  714. // sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and done = 1",RQ_YDTQuestion_Module.car_type];
  715. // }
  716. // }
  717. NSInteger cnt = [shareDataBase intForQuery:sql];
  718. [shareDataBase close];
  719. //NSLog(@"这里查题目数量--sql-->%@-------where-->%@------count-->%d",sql,where,(int)cnt);
  720. return cnt;
  721. }
  722. return 0;
  723. }
  724. +(int)getProgress
  725. {
  726. int num=0;
  727. shareDataBase=[DB_Que_Helper createDataBase];
  728. if ([shareDataBase open]) {
  729. FMResultSet *s = [shareDataBase executeQuery:@"SELECT * FROM db_versions WHERE id=1"];
  730. if ([s next]) {
  731. num=[[s stringForColumn:@"version"] intValue];
  732. }
  733. [shareDataBase close];
  734. }
  735. return num;
  736. }
  737. +(NSArray *)queryColumnNameWithTable:(NSString *)tableName
  738. {
  739. sqlite3 *database;
  740. sqlite3_open([dataBasePath2 UTF8String], &database);
  741. sqlite3_stmt *statement;
  742. //获取数据库的表的名字
  743. // const char *getTableInfo = "select * from sqlite_master where type='table' order by name";
  744. // sqlite3_prepare_v2(database, getTableInfo, -1, &statement, nil);
  745. // while (sqlite3_step(statement) == SQLITE_ROW) {
  746. // char *nameData = (char *)sqlite3_column_text(statement, 1);
  747. // NSString *tableName = [[NSString alloc] initWithUTF8String:nameData];
  748. // NSLog(@"name:%@",tableName);
  749. // }
  750. NSMutableArray *array = [NSMutableArray array];
  751. NSString *columnName = @"";
  752. //获取某表中字段的名字
  753. const char *getColumn = "PRAGMA table_info(question)";
  754. sqlite3_prepare_v2(database, getColumn, -1, &statement, nil);
  755. while (sqlite3_step(statement) == SQLITE_ROW) {
  756. char *nameData = (char *)sqlite3_column_text(statement, 1);
  757. columnName = [[NSString alloc] initWithUTF8String:nameData];
  758. //NSLog(@"columnName:%@",columnName);
  759. [array addObject:columnName];
  760. }
  761. sqlite3_finalize(statement);
  762. return array;
  763. }
  764. #pragma mark markQuestion
  765. //(_id integer PRIMARY KEY, type text, isUpdata text, subject text ,car_type text ,sfzhm text)
  766. +(BOOL)setMarkQuestionWithQuestion:(NSInteger)qId Type:(NSString *)markType State:(NSInteger)state
  767. {
  768. BOOL isOk = NO;
  769. shareDataBase=[DB_Que_Helper createDataBase];
  770. if ([shareDataBase open]) {
  771. if (state == 1) {
  772. //添加
  773. NSString *sfzhm = @"no";
  774. if (myDelegate.isLogin) {
  775. sfzhm = RQ_USER_MANAGER.currentUser._id;
  776. }
  777. //在调这个方法前已经判断过 是否已经存在
  778. isOk = [shareDataBase executeUpdate:[NSString stringWithFormat:@"insert into markQuestion (_id,type,isUpdata,subject,car_type,sfzhm)values(%ld,'%@','0','%@','%@','%@')",(long)qId,markType,myDelegate.subject,RQ_YDTQuestion_Module.car_type,sfzhm]];
  779. }
  780. if (state == 0) {
  781. //删除
  782. NSString *sfzhm = @"no";
  783. if (myDelegate.isLogin) {
  784. sfzhm = RQ_USER_MANAGER.currentUser._id;
  785. }
  786. isOk = [shareDataBase executeUpdate:[NSString stringWithFormat:@"delete from markQuestion where _id=%ld and type='%@' and sfzhm='%@'",(long)qId,markType,sfzhm]];
  787. }
  788. [shareDataBase close];
  789. }
  790. return isOk;
  791. }
  792. +(BOOL)queryIsHaveWithQuestion:(NSInteger)qId Type:(NSString *)markType
  793. {
  794. BOOL isOk = NO;
  795. shareDataBase=[DB_Que_Helper createDataBase];
  796. if ([shareDataBase open]) {
  797. NSString *sfzhm = @"no";
  798. if (myDelegate.isLogin) {
  799. sfzhm = RQ_USER_MANAGER.currentUser._id;
  800. }
  801. NSInteger cnt = [shareDataBase intForQuery:[NSString stringWithFormat:@"select count(*) from markQuestion where _id=%ld and type='%@' and subject = '%@' and car_type = '%@' and sfzhm = '%@'",(long)qId,markType,myDelegate.subject,RQ_YDTQuestion_Module.car_type,sfzhm]];
  802. if (cnt > 0) {
  803. isOk = YES;
  804. }
  805. [shareDataBase close];
  806. }
  807. return isOk;
  808. }
  809. +(NSArray *)queryMarkQuestionWithType:(NSString *)markType
  810. {
  811. NSMutableArray *questions = [[NSMutableArray alloc]init];
  812. shareDataBase=[DB_Que_Helper createDataBase];
  813. if ([shareDataBase open]) {
  814. NSString *sfzhm = @"no";
  815. if (myDelegate.isLogin) {
  816. sfzhm = RQ_USER_MANAGER.currentUser._id;
  817. }
  818. NSString *sql;//1、错题 2、收藏 3、排错 4、已做未做 5、除去排除的题
  819. if ([markType isEqualToString:@"4"]){
  820. sql=[NSString stringWithFormat:@"select * from question where subject = '%@' and car_type = '%@' and _id not in (select _id from markQuestion where type = '4' and subject = '%@' and car_type = '%@' and sfzhm = '%@')",myDelegate.subject,RQ_YDTQuestion_Module.car_type,myDelegate.subject,RQ_YDTQuestion_Module.car_type,sfzhm];
  821. }else if ([markType isEqualToString:@"5"]){
  822. //除去排错剩下的所有题
  823. sql=[NSString stringWithFormat:@"select * from question where subject = '%@' and car_type = '%@' and _id not in (select _id from markQuestion where type = '3' and subject = '%@' and car_type = '%@' and sfzhm = '%@')",myDelegate.subject,RQ_YDTQuestion_Module.car_type,myDelegate.subject,RQ_YDTQuestion_Module.car_type,sfzhm];
  824. if ([myDelegate.subject isEqualToString:@"4"])
  825. {
  826. sql=[NSString stringWithFormat:@"select * from question where subject = '%@' and car_type = 'C1' and _id not in (select _id from markQuestion where type = '3' and subject = '%@' and car_type = '%@' and sfzhm = '%@')",myDelegate.subject,myDelegate.subject,RQ_YDTQuestion_Module.car_type,sfzhm];
  827. if ([RQ_YDTQuestion_Module.car_type isEqualToString:@"D"]) {
  828. sql=[NSString stringWithFormat:@"select * from question where subject = '%@' and car_type = 'D' and _id not in (select _id from markQuestion where type = '3' and subject = '%@' and car_type = '%@' and sfzhm = '%@')",myDelegate.subject,myDelegate.subject,RQ_YDTQuestion_Module.car_type,sfzhm];
  829. }
  830. }
  831. }else{
  832. sql=[NSString stringWithFormat:@"select * from question where _id in (select _id from markQuestion where type = '%@' and subject = '%@' and car_type = '%@' and sfzhm = '%@')",markType,myDelegate.subject,RQ_YDTQuestion_Module.car_type,sfzhm];
  833. }
  834. //如果是资格证 就将科目的判断去掉
  835. if ([RQ_YDTQuestion_Module.car_type isEqualToString:@"1"] || [RQ_YDTQuestion_Module.car_type isEqualToString:@"2"] || [RQ_YDTQuestion_Module.car_type isEqualToString:@"3"] || [RQ_YDTQuestion_Module.car_type isEqualToString:@"4"] || [RQ_YDTQuestion_Module.car_type isEqualToString:@"5"])
  836. {
  837. if ([sql containsString:[NSString stringWithFormat:@"subject = '%@' and",myDelegate.subject]]) {
  838. sql = [sql stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"subject = '%@' and",myDelegate.subject] withString:@""];
  839. }
  840. }
  841. FMResultSet *s=[shareDataBase executeQuery:sql];
  842. questions = [Question questionsWithFMResultSet:s];
  843. // NSLog(@"这里找出要显示的题目sql-->%@------------count-->%d",sql,(int)questions.count);
  844. [shareDataBase close];
  845. }
  846. return questions;
  847. }
  848. +(NSArray *)queryMarkQuestionWithUpData:(NSInteger)isUpdata MarkType:(NSString *)markType Subject:(NSString *)sub
  849. {
  850. NSMutableArray<NSString *> *array = [NSMutableArray array];
  851. shareDataBase=[DB_Que_Helper createDataBase];
  852. if ([shareDataBase open]) {
  853. NSString *sfzhm = @"no";
  854. if (myDelegate.isLogin) {
  855. sfzhm = RQ_USER_MANAGER.currentUser._id;
  856. }
  857. NSString *sql;
  858. sql=[NSString stringWithFormat:@"select * from markQuestion where subject = '%@' and car_type = '%@' and type = '%@' and isUpdata = '0' and sfzhm = '%@'",sub,RQ_YDTQuestion_Module.car_type,markType,sfzhm];
  859. if (isUpdata == 1) {
  860. sql = [sql stringByReplacingOccurrencesOfString:@" and isUpdata = '0'" withString:@""];
  861. }
  862. FMResultSet *s=[shareDataBase executeQuery:sql];
  863. while ([s next]) {
  864. NSString *questId = [s stringForColumn:@"_id"];
  865. [array addObject:questId];
  866. }
  867. [shareDataBase close];
  868. }
  869. //NSLog(@"题目---><>%@",array);
  870. return array;
  871. }
  872. + (NSInteger)queryMarkQuestionWithMarkType:(NSString *)markType {
  873. NSInteger cnt = 0;
  874. shareDataBase=[DB_Que_Helper createDataBase];
  875. if ([shareDataBase open]) {
  876. NSString *sfzhm = @"no";
  877. if (myDelegate.isLogin) {
  878. sfzhm = RQ_USER_MANAGER.currentUser._id;
  879. }
  880. NSString *queryStr = [NSString stringWithFormat:@"select count(*) from markQuestion where subject = '%@' and car_type = '%@' and sfzhm = '%@' and type = '%@'",myDelegate.subject,RQ_YDTQuestion_Module.car_type,sfzhm,markType];
  881. cnt = [shareDataBase intForQuery:queryStr];
  882. [shareDataBase close];
  883. }
  884. return cnt;
  885. }
  886. +(BOOL)setIsUpdateWithType:(NSString *)markType
  887. {
  888. BOOL isOk = NO;
  889. shareDataBase=[DB_Que_Helper createDataBase];
  890. if ([shareDataBase open]) {
  891. NSString *sfzhm = @"no";
  892. if (myDelegate.isLogin) {
  893. sfzhm = RQ_USER_MANAGER.currentUser._id;
  894. }
  895. NSString *sql;
  896. if ([markType isEqualToString:@"0"]) {
  897. sql = [NSString stringWithFormat:@"update markQuestion set isUpdata = '1' where subject = '%@' and car_type = '%@' and sfzhm = '%@'",myDelegate.subject,RQ_YDTQuestion_Module.car_type,sfzhm];
  898. }else{
  899. sql = [NSString stringWithFormat:@"update markQuestion set isUpdata = '1' where type = '%@' and subject = '%@' and car_type = '%@' and sfzhm = '%@'",markType,myDelegate.subject,RQ_YDTQuestion_Module.car_type,sfzhm];
  900. }
  901. isOk = [shareDataBase executeUpdate:sql];
  902. [shareDataBase close];
  903. }
  904. return isOk;
  905. }
  906. +(BOOL)setIsUpdateWithArray:(NSArray<NSString *> *)array
  907. {
  908. BOOL isOk = NO;
  909. shareDataBase=[DB_Que_Helper createDataBase];
  910. if ([shareDataBase open]) {
  911. NSString *sfzhm = @"no";
  912. if (myDelegate.isLogin) {
  913. sfzhm = RQ_USER_MANAGER.currentUser._id;
  914. }
  915. NSString *sql;
  916. NSString *sub1 = @"";
  917. NSString *sub4 = @"";
  918. for (NSString *tagStr in array) {
  919. NSInteger tag = [tagStr integerValue];
  920. NSString *typeStr = @"1";
  921. if (tag == 3 || tag == 6) {
  922. typeStr = @"2";
  923. }
  924. if (tag == 4 || tag == 7) {
  925. typeStr = @"3";
  926. }
  927. if (tag < 5) {//科一
  928. sub1 = [sub1 stringByAppendingString:[NSString stringWithFormat:@"%@,",typeStr]];
  929. }else{//科四
  930. sub4 = [sub4 stringByAppendingString:[NSString stringWithFormat:@"%@,",typeStr]];
  931. }
  932. }
  933. if (sub1.length > 0) {
  934. sub1 = [sub1 substringToIndex:sub1.length - 1];
  935. sql = [NSString stringWithFormat:@"update markQuestion set isUpdata = '1' where subject = '1' and car_type = '%@' and sfzhm = '%@' and type in (%@)",RQ_YDTQuestion_Module.car_type,sfzhm,sub1];
  936. isOk = [shareDataBase executeUpdate:sql];
  937. }
  938. if (sub4.length > 0) {
  939. sub4 = [sub4 substringToIndex:sub4.length - 1];
  940. sql = [NSString stringWithFormat:@"update markQuestion set isUpdata = '1' where subject = '4' and car_type = '%@' and sfzhm = '%@' and type in (%@)",RQ_YDTQuestion_Module.car_type,sfzhm,sub4];
  941. isOk = [shareDataBase executeUpdate:sql];
  942. }
  943. [shareDataBase close];
  944. }
  945. return isOk;
  946. }
  947. +(BOOL)setMarkQuestionWithArray:(NSArray *)questions
  948. {
  949. BOOL isOk = NO;
  950. BOOL allIsOK = YES;
  951. NSString *sfzhm = @"no";
  952. if (myDelegate.isLogin) {
  953. sfzhm = RQ_USER_MANAGER.currentUser._id;
  954. }
  955. //NSLog(@"-----><>%@",questions);
  956. shareDataBase=[DB_Que_Helper createDataBase];
  957. if ([shareDataBase open]) {
  958. for (NSDictionary *obj in questions) {
  959. NSInteger qId = [obj[@"QID"] integerValue];
  960. NSString *kType = obj[@"TYPE"];
  961. NSString *markType;
  962. if ([kType isEqualToString:@"1"]){
  963. //收藏
  964. markType = @"2";
  965. }else if ([kType isEqualToString:@"2"]){
  966. //错题
  967. markType = @"1";
  968. }else{
  969. //排除
  970. markType = @"3";
  971. }
  972. NSString *cartype = RQ_YDTQuestion_Module.car_type;
  973. if ([obj[@"CARTYPE"] length] > 0) {
  974. cartype = obj[@"CARTYPE"];
  975. }
  976. NSString *sub = myDelegate.subject;
  977. if ([obj[@"SUBJECT"] length] > 0) {
  978. sub = obj[@"SUBJECT"];
  979. }
  980. if ([markType isEqualToString:@"1"]) {
  981. NSInteger cnt = [shareDataBase intForQuery:[NSString stringWithFormat:@"select count(*) from markQuestion where _id=%ld and type='4' and subject = '%@' and car_type = '%@' and sfzhm = '%@'",(long)qId,sub,cartype,sfzhm]];
  982. if (cnt == 0) {
  983. [shareDataBase executeUpdate:[NSString stringWithFormat:@"insert into markQuestion (_id,type,isUpdata,subject,car_type,sfzhm)values(%ld,'4','1','%@','%@','%@')",(long)qId,sub,cartype,sfzhm]];
  984. }
  985. }
  986. //dansonmark这里的科目 车型应该来自服务器 如果服务器不返回 就要从本地题目库查
  987. isOk = [shareDataBase executeUpdate:[NSString stringWithFormat:@"insert into markQuestion (_id,type,isUpdata,subject,car_type,sfzhm)values(%ld,'%@','1','%@','%@','%@')",(long)qId,markType,sub,cartype,sfzhm]];
  988. if (isOk == NO) {
  989. allIsOK = NO;
  990. }
  991. }
  992. [shareDataBase close];
  993. }
  994. return allIsOK;
  995. }
  996. +(BOOL)deleteAllMarkQuestion
  997. {
  998. BOOL isOk = NO;
  999. NSString *sfzhm = @"no";
  1000. if (myDelegate.isLogin) {
  1001. sfzhm = RQ_USER_MANAGER.currentUser._id;
  1002. }
  1003. shareDataBase=[DB_Que_Helper createDataBase];
  1004. if ([shareDataBase open]) {
  1005. isOk = [shareDataBase executeUpdate:[NSString stringWithFormat:@"delete from markQuestion where type != '4' and sfzhm='%@'",sfzhm]];
  1006. [shareDataBase close];
  1007. }
  1008. return isOk;
  1009. }
  1010. +(void)moveIsDoneToMarkQuestion
  1011. {
  1012. //等以后就版本没有了 就不用管这个了
  1013. NSArray *columnArray = [DB_Que_Helper queryColumnNameWithTable:@"question"];
  1014. if (![columnArray containsObject:@"done"]) {
  1015. //说明是新版 没有了done字段。不需要转移已做题
  1016. return;
  1017. }
  1018. NSString *sfzhm = @"no";
  1019. if (myDelegate.isLogin) {
  1020. sfzhm = RQ_USER_MANAGER.currentUser._id;
  1021. }
  1022. shareDataBase=[DB_Que_Helper createDataBase];
  1023. if ([shareDataBase open]) {
  1024. FMResultSet *s=[shareDataBase executeQuery:@"select * from question where done = 1"];
  1025. NSMutableArray *questions = [Question questionsWithFMResultSet:s];
  1026. for (Question *question in questions) {
  1027. //NSLog(@"插入已做题");
  1028. [shareDataBase executeUpdate:[NSString stringWithFormat:@"insert into markQuestion (_id,type,isUpdata,subject,car_type,sfzhm)values(%d,'4','0','%@','%@','%@')",[question._id intValue],myDelegate.subject,RQ_YDTQuestion_Module.car_type,sfzhm]];
  1029. }
  1030. [shareDataBase close];
  1031. }
  1032. }
  1033. + (void)updateQuestionWithId:(NSString *)idStr {
  1034. NSString *sqlStr0 = [NSString stringWithFormat:@"UPDATE question SET question = '%@' WHERE _id = %ld",self.updateDataDic[idStr][@"question"],(long)idStr.integerValue];
  1035. NSString *sqlStr1 = [NSString stringWithFormat:@"UPDATE question SET explain = '%@' WHERE _id = %ld",self.updateDataDic[idStr][@"explain"],(long)idStr.integerValue];
  1036. BOOL isSuccess0 = [shareDataBase executeUpdate:sqlStr0];
  1037. BOOL isSuccess1 = [shareDataBase executeUpdate:sqlStr1];
  1038. NSLog(@"%@ %@",isSuccess0? @"Success!" : @"Failed!",isSuccess1? @"Success!" : @"Failed!");
  1039. }
  1040. + (NSDictionary *)updateDataDic {
  1041. return @{
  1042. @"332" : @{
  1043. @"question" : @"年龄在70周岁以上的驾驶人多长时间提交一次身体条件证明?",
  1044. @"explain" : @"新修改的《机动车驾驶证申领和使用规定》还放宽了未体检注销驾驶证后恢复驾驶资格的年龄,规定:年龄在70周岁以上的驾驶人,在一个记分周期结束后一年内未提交身体条件证明的,车辆管理所将注销其机动车驾驶证。",
  1045. },
  1046. @"466" : @{
  1047. @"question" : @"年龄在70周岁以上,在一个记分周期结束后一年内未提交身体条件证明的,其机动车驾驶证将会被车辆管理所注销。",
  1048. @"explain" : @"新修改的《机动车驾驶证申领和使用规定》还放宽了未体检注销驾驶证后恢复驾驶资格的年龄,规定:年龄在70周岁以上的驾驶人,在一个记分周期结束后一年内未提交身体条件证明的,车辆管理所将注销其机动车驾驶证。",
  1049. },
  1050. @"521" : @{
  1051. @"question" : @"年龄在70周岁以上的机动车驾驶人,应当每年进行一次身体检查的目的是什么?",
  1052. @"explain" : @"新修改的《机动车驾驶证申领和使用规定》还放宽了未体检注销驾驶证后恢复驾驶资格的年龄,规定:年龄在70周岁以上的驾驶人,在一个记分周期结束后一年内未提交身体条件证明的,车辆管理所将注销其机动车驾驶证。",
  1053. },
  1054. @"3960" : @{
  1055. @"question" : @"申请小型汽车准驾车型驾驶证的人年龄条件是多少?",
  1056. @"explain" : @"《公安部令第123号》第十一条:申请小型汽车、小型自动挡汽车、残疾人专用小型自动挡载客汽车、轻便摩托车准驾车型的,在18周岁以上、70周岁以下。",
  1057. },
  1058. @"3962" : @{
  1059. @"question" : @"申请小型汽车汽车驾驶证的,年龄应在18周岁以上70周岁以下。",
  1060. @"explain" : @"《公安部令第123号》第十一条:申请小型汽车、小型自动挡汽车、残疾人专用小型自动挡载客汽车、轻便摩托车准驾车型的,在18周岁以上、70周岁以下。",
  1061. },
  1062. @"3976" : @{
  1063. @"question" : @"年龄在70周岁以上的驾驶人多长时间提交一次身体条件证明?",
  1064. @"explain" : @"新修改的《机动车驾驶证申领和使用规定》还放宽了未体检注销驾驶证后恢复驾驶资格的年龄,规定:年龄在70周岁以上的驾驶人,在一个记分周期结束后一年内未提交身体条件证明的,车辆管理所将注销其机动车驾驶证。",
  1065. },
  1066. @"2738" : @{
  1067. @"question" : @"申请小型汽车准驾车型驾驶证的人年龄条件是多少?",
  1068. @"explain" : @"《公安部令第123号》第十一条:申请小型汽车、小型自动挡汽车、残疾人专用小型自动挡载客汽车、轻便摩托车准驾车型的,在18周岁以上、70周岁以下。",
  1069. },
  1070. @"2740" : @{
  1071. @"question" : @"申请小型汽车汽车驾驶证的,年龄应在18周岁以上70周岁以下。",
  1072. @"explain" : @"《公安部令第123号》第十一条:申请小型汽车、小型自动挡汽车、残疾人专用小型自动挡载客汽车、轻便摩托车准驾车型的,在18周岁以上、70周岁以下。",
  1073. },
  1074. @"2754" : @{
  1075. @"question" : @"年龄在70周岁以上的驾驶人多长时间提交一次身体条件证明?",
  1076. @"explain" : @"新修改的《机动车驾驶证申领和使用规定》还放宽了未体检注销驾驶证后恢复驾驶资格的年龄,规定:年龄在70周岁以上的驾驶人,在一个记分周期结束后一年内未提交身体条件证明的,车辆管理所将注销其机动车驾驶证。",
  1077. },
  1078. @"3627" : @{
  1079. @"question" : @"年龄在70周岁以上,在1个记分周期结束后,( )内未提交身体检查结果的,车辆管理所注销其驾驶证。",
  1080. @"explain" : @"新修改的《机动车驾驶证申领和使用规定》还放宽了未体检注销驾驶证后恢复驾驶资格的年龄,规定:年龄在70周岁以上的驾驶人,在一个记分周期结束后一年内未提交身体条件证明的,车辆管理所将注销其机动车驾驶证。",
  1081. },
  1082. @"3631" : @{
  1083. @"question" : @"年龄在60岁以上,超过机动车驾驶证有效期一年以上未换证的,车辆管理所将注销其驾驶证。",
  1084. @"explain" : @"《公安部令第123号》第六十七条:机动车驾驶人具有下列情形之一的,车辆管理所应当注销其机动车驾驶证:(七)超过机动车驾驶证有效期一年以上未换证的。",
  1085. },
  1086. @"3595" : @{
  1087. @"question" : @"年龄在60岁以上持有准驾车型为大型客车的驾驶人,应当到驾驶证核发地车辆管理所换领准驾车型为小型汽车或小型自动挡汽车的驾驶证。",
  1088. @"explain" : @"《公安部令第123号》第五十条:年龄在60周岁以上的,不得驾驶大型客车、牵引车、城市公交车、中型客车、大型货车、无轨电车和有轨电车;持有大型客车、牵引车、城市公交车、中型客车、大型货车驾驶证的,应当到机动车驾驶证核发地车辆管理所换领准驾车型为小型汽车或者小型自动挡汽车的机动车驾驶证。",
  1089. },
  1090. };
  1091. }
  1092. + (NSArray *)updateIdArr {
  1093. return @[@"332", @"466", @"521", @"3960", @"3962", @"3976", @"2738", @"2740", @"2754", @"3627", @"3631", @"3595"];
  1094. }
  1095. @end