DB_Que_Helper.m 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  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.state=[s stringForColumn:@"state"];
  427. question.subject=[s stringForColumn:@"subject"];
  428. [questions addObject:question];
  429. }
  430. [shareDataBase close];
  431. }else{
  432. NSLog(@"题库获取失败,数据库未打开");
  433. }
  434. return questions;
  435. }
  436. +(NSArray*)queryArea
  437. {
  438. /**从数据库找地址信息
  439. */
  440. NSMutableArray *questions=[[NSMutableArray alloc] init];
  441. shareDataBase=[DB_Que_Helper createDataBase];
  442. if ([shareDataBase open]) {
  443. FMResultSet *s=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from area"]];
  444. while ([s next]) {
  445. NSMutableDictionary* question = [NSMutableDictionary dictionary];
  446. question[@"name"] = [s stringForColumn:@"name"];
  447. question[@"code"] = [s stringForColumn:@"code"];
  448. question[@"parent_code"] = [s stringForColumn:@"parent_code"];
  449. [questions addObject:question];
  450. }
  451. [shareDataBase close];
  452. }else{
  453. NSLog(@"ERROR: db unOpen");
  454. }
  455. return questions;
  456. }
  457. +(NSArray *)queryQuestion:(NSString *)where
  458. {
  459. NSMutableArray *questions=[[NSMutableArray alloc]init];
  460. shareDataBase=[DB_Que_Helper createDataBase];
  461. if ([shareDataBase open]) {
  462. NSString *sql;
  463. //章节考试用到
  464. if ([where rangeOfString:@"subject = '1'"].location != NSNotFound)
  465. {
  466. sql=[NSString stringWithFormat:@"select * from question where car_type = '%@' and %@",RQ_YDTQuestion_Module.car_type,where];
  467. }
  468. if ([where rangeOfString:@"subject = '4'"].location != NSNotFound)
  469. {
  470. sql=[NSString stringWithFormat:@"select * from question where car_type = 'C1' and %@",where ];
  471. if ([RQ_YDTQuestion_Module.car_type isEqualToString:@"D"]) {
  472. sql=[NSString stringWithFormat:@"select * from question where car_type = 'D' and %@",where ];
  473. }
  474. }
  475. //资格证 非强化 非全部题
  476. 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"])
  477. {
  478. if (![where isEqualToString:@"strong"])
  479. {
  480. if (where.length > 12) {
  481. where = [where substringFromIndex:13];
  482. }
  483. sql=[NSString stringWithFormat:@"select * from question where car_type = '%@'%@",RQ_YDTQuestion_Module.car_type,where];
  484. }
  485. }
  486. if ([where isEqualToString:@"strong"])//代表强化练习
  487. {
  488. sql=[NSString stringWithFormat:@"select * from question where %@",myDelegate.where];
  489. }
  490. FMResultSet *s=[shareDataBase executeQuery:sql];
  491. questions = [Question questionsWithFMResultSet:s];
  492. //NSLog(@"这里找出要显示的题目sql-->%@-------where-->%@------count-->%d",sql,where,(int)questions.count);
  493. [shareDataBase close];
  494. }
  495. return questions;
  496. }
  497. //查找章节列表
  498. +(NSArray *)queryList:(NSString *)where
  499. {
  500. NSMutableArray *lists=[[NSMutableArray alloc]init];
  501. shareDataBase=[DB_Que_Helper createDataBase];
  502. if ([shareDataBase open]) {
  503. FMResultSet *s=[shareDataBase executeQuery:where];
  504. while ([s next])
  505. {
  506. NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:[s stringForColumn:@"title"],@"title",[s stringForColumn:@"ID"],@"chapter", nil];
  507. [lists addObject:dic];
  508. }
  509. [shareDataBase close];
  510. }
  511. return lists;
  512. }
  513. //查找标识
  514. +(NSArray *)queryMark:(NSString *)where
  515. {
  516. NSMutableArray *markArray = [NSMutableArray array];
  517. shareDataBase=[DB_Que_Helper createDataBase];
  518. if ([shareDataBase open]) {
  519. FMResultSet *s=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from APP_MARK_GROUP where MG_PARENT = '%@'",where]];
  520. markArray = [markGroup markGroupWithFMResultSet:s];
  521. [shareDataBase close];
  522. }
  523. return markArray;
  524. }
  525. +(NSArray *)queryDetailMark:(NSString *)where
  526. {
  527. NSMutableArray *markArray = [NSMutableArray array];
  528. shareDataBase=[DB_Que_Helper createDataBase];
  529. if ([shareDataBase open]) {
  530. FMResultSet *s=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from APP_MARK_INFO where %@",where]];
  531. markArray = [markInfo markInfoWithFMResultSet:s];
  532. [shareDataBase close];
  533. }
  534. return markArray;
  535. }
  536. +(BOOL)updateDB:(NSString *)sql
  537. {
  538. shareDataBase=[DB_Que_Helper createDataBase];
  539. BOOL isOk = NO;
  540. if ([shareDataBase open])
  541. {
  542. isOk=[shareDataBase executeUpdate:sql];
  543. [shareDataBase close];
  544. }
  545. return isOk;
  546. }
  547. //添加已做的题
  548. +(void)addToDoneQue:(NSInteger)ID
  549. {
  550. shareDataBase=[DB_Que_Helper createDataBase];
  551. if ([shareDataBase open]) {
  552. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update question set done = 1 where _id=%d",(int)ID]];
  553. [shareDataBase close];
  554. }
  555. }
  556. //这里也错了 和数据库字段不符 delete --数据库问题--danson
  557. +(BOOL)addToStrengthenQue:(NSInteger)ID
  558. {
  559. BOOL isOk = NO;
  560. shareDataBase=[DB_Que_Helper createDataBase];
  561. if ([shareDataBase open]) {
  562. isOk=[shareDataBase executeUpdate:[NSString stringWithFormat:@"update question set state = 1 where _id=%d",(int)ID]];
  563. [shareDataBase close];
  564. }
  565. return isOk;
  566. }
  567. +(BOOL)removeToStrengthenQue:(NSInteger)ID
  568. {
  569. BOOL isOk = NO;
  570. shareDataBase=[DB_Que_Helper createDataBase];
  571. if ([shareDataBase open]) {
  572. isOk=[shareDataBase executeUpdate:[NSString stringWithFormat:@"update question set state = 0 where _id=%d",(int)ID]];
  573. [shareDataBase close];
  574. }
  575. return isOk;
  576. }
  577. /**
  578. 抽考试题
  579. */
  580. +(NSArray *)queryExamQuestion:(NSString *)where
  581. {
  582. NSMutableArray *questions=[[NSMutableArray alloc]init];
  583. shareDataBase=[DB_Que_Helper createDataBase];
  584. if ([shareDataBase open]) {
  585. NSString *sql;
  586. //模拟考试中....
  587. //从数据库中随机抽取相应数目的题目 应该不用考虑题目存放数组位置问题吧 因为不是耗时操作 按代码先后顺序来排序
  588. //资格证考试和科一科四不同 40单选 40判断 10多选
  589. //科目一40题判断题 科目四20题判断题
  590. if ([where isEqualToString:@"subject = '1'"])
  591. {
  592. 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"];
  593. }
  594. else if ([where isEqualToString:@"subject = '4'"])
  595. {
  596. sql=[NSString stringWithFormat:@"select * from question where car_type = 'C1' and subject = 4 and option_type = 0 order by random() limit 20"];
  597. }
  598. else
  599. {
  600. sql=[NSString stringWithFormat:@"select * from question where car_type = '%@' and option_type = 1 order by random() limit 40",RQ_YDTQuestion_Module.car_type];
  601. }
  602. FMResultSet *s=[shareDataBase executeQuery:sql];
  603. [questions addObjectsFromArray:[Question questionsWithFMResultSet:s]];
  604. //科目一60题单选题 科目四20题单选题
  605. if ([where isEqualToString:@"subject = '1'"])
  606. {
  607. 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"];
  608. }
  609. else if ([where isEqualToString:@"subject = '4'"])
  610. {
  611. sql=[NSString stringWithFormat:@"select * from question where car_type = 'C1' and subject = 4 and option_type = 1 order by random() limit 20"];
  612. }
  613. else
  614. {
  615. sql=[NSString stringWithFormat:@"select * from question where car_type = '%@' and option_type = 0 order by random() limit 40",RQ_YDTQuestion_Module.car_type];
  616. }
  617. FMResultSet *s2=[shareDataBase executeQuery:sql];
  618. [questions addObjectsFromArray:[Question questionsWithFMResultSet:s2]];
  619. // 科目四 10题多选题(科目一无多选题)
  620. if ([where isEqualToString:@"subject = '4'"])
  621. {
  622. 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"]];
  623. [questions addObjectsFromArray:[Question questionsWithFMResultSet:s3]];
  624. }
  625. if ([where isEqualToString:@"subject = nil"])
  626. {
  627. //NSLog(@"资格证多选题");
  628. 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]];
  629. [questions addObjectsFromArray:[Question questionsWithFMResultSet:s3]];
  630. }
  631. [shareDataBase close];
  632. }
  633. return questions;
  634. }
  635. //清空错题、收藏、排错
  636. +(void)clearFavInfo
  637. {
  638. //每次进程序都要有这样的操作
  639. shareDataBase=[DB_Que_Helper createDataBase];
  640. if([shareDataBase open])
  641. {
  642. [shareDataBase executeUpdate:@"update question set state = 0 where state = 1"];
  643. [shareDataBase executeUpdate:@"update question set state = 0 where iscollect = 1"];
  644. [shareDataBase executeUpdate:@"update question set state = 0 where isdone = 1"];
  645. [shareDataBase close];
  646. return;
  647. }else{
  648. NSLog(@"db error");
  649. return;
  650. }
  651. }
  652. +(BOOL)executeSql:(NSArray *)sqlList
  653. {
  654. shareDataBase=[DB_Que_Helper createDataBase];
  655. if([shareDataBase open]){
  656. NSInteger count=sqlList.count-1;
  657. for(int i=0;i<count;i++){
  658. [shareDataBase executeUpdate:[sqlList objectAtIndex:i]];
  659. }
  660. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update db_versions set version = %@ where id=1",[sqlList objectAtIndex:count]]];
  661. [shareDataBase close];
  662. return YES;
  663. }else{
  664. return NO;
  665. }
  666. }
  667. /**
  668. 从选择页面过来查询多少题目
  669. */
  670. +(NSInteger)countForQuery:(NSString *)where
  671. {
  672. shareDataBase=[DB_Que_Helper createDataBase];
  673. if ([shareDataBase open]) {
  674. NSString *sql;
  675. if ([where isEqualToString:@"subject = 1"])
  676. {
  677. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and %@",RQ_YDTQuestion_Module.car_type,where];
  678. }
  679. else if ([where isEqualToString:@"subject = 4"])
  680. {
  681. //NSLog(@"摩托车科是--%@",RQ_YDTQuestion_Module.car_type);
  682. if ([RQ_YDTQuestion_Module.car_type isEqualToString:@"D"]) {
  683. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = 'D' and %@",where];
  684. }else{
  685. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = 'C1' and %@",where];
  686. }
  687. }
  688. else if ([where isEqualToString:@"nil"])
  689. {
  690. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@'",RQ_YDTQuestion_Module.car_type];
  691. }
  692. else if ([where isEqualToString:@"state = 1 and subject = 1"])
  693. {
  694. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and %@",RQ_YDTQuestion_Module.car_type,where];
  695. }
  696. else if ([where isEqualToString:@"state = 1 and subject = 4"])
  697. {
  698. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = 'C1' and %@",where ];
  699. }
  700. else
  701. {
  702. sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and state = 1",RQ_YDTQuestion_Module.car_type];
  703. }
  704. if ([where rangeOfString:@"strong"].location != NSNotFound)
  705. {
  706. sql=[NSString stringWithFormat:@"select count(*) from question where %@",myDelegate.where];
  707. }
  708. // if ([where isEqualToString:@"done"])
  709. // {
  710. // sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and done = 1 and %@",RQ_YDTQuestion_Module.car_type,myDelegate.where];
  711. // if ([myDelegate.where isEqualToString:@"done"])
  712. // {
  713. // sql=[NSString stringWithFormat:@"select count(*) from question where car_type = '%@' and done = 1",RQ_YDTQuestion_Module.car_type];
  714. // }
  715. // }
  716. NSInteger cnt = [shareDataBase intForQuery:sql];
  717. [shareDataBase close];
  718. //NSLog(@"这里查题目数量--sql-->%@-------where-->%@------count-->%d",sql,where,(int)cnt);
  719. return cnt;
  720. }
  721. return 0;
  722. }
  723. +(int)getProgress
  724. {
  725. int num=0;
  726. shareDataBase=[DB_Que_Helper createDataBase];
  727. if ([shareDataBase open]) {
  728. FMResultSet *s = [shareDataBase executeQuery:@"SELECT * FROM db_versions WHERE id=1"];
  729. if ([s next]) {
  730. num=[[s stringForColumn:@"version"] intValue];
  731. }
  732. [shareDataBase close];
  733. }
  734. return num;
  735. }
  736. +(NSArray *)queryColumnNameWithTable:(NSString *)tableName
  737. {
  738. sqlite3 *database;
  739. sqlite3_open([dataBasePath2 UTF8String], &database);
  740. sqlite3_stmt *statement;
  741. //获取数据库的表的名字
  742. // const char *getTableInfo = "select * from sqlite_master where type='table' order by name";
  743. // sqlite3_prepare_v2(database, getTableInfo, -1, &statement, nil);
  744. // while (sqlite3_step(statement) == SQLITE_ROW) {
  745. // char *nameData = (char *)sqlite3_column_text(statement, 1);
  746. // NSString *tableName = [[NSString alloc] initWithUTF8String:nameData];
  747. // NSLog(@"name:%@",tableName);
  748. // }
  749. NSMutableArray *array = [NSMutableArray array];
  750. NSString *columnName = @"";
  751. //获取某表中字段的名字
  752. const char *getColumn = "PRAGMA table_info(question)";
  753. sqlite3_prepare_v2(database, getColumn, -1, &statement, nil);
  754. while (sqlite3_step(statement) == SQLITE_ROW) {
  755. char *nameData = (char *)sqlite3_column_text(statement, 1);
  756. columnName = [[NSString alloc] initWithUTF8String:nameData];
  757. //NSLog(@"columnName:%@",columnName);
  758. [array addObject:columnName];
  759. }
  760. sqlite3_finalize(statement);
  761. return array;
  762. }
  763. #pragma mark markQuestion
  764. //(_id integer PRIMARY KEY, type text, isUpdata text, subject text ,car_type text ,sfzhm text)
  765. +(BOOL)setMarkQuestionWithQuestion:(NSInteger)qId Type:(NSString *)markType State:(NSInteger)state
  766. {
  767. BOOL isOk = NO;
  768. shareDataBase=[DB_Que_Helper createDataBase];
  769. if ([shareDataBase open]) {
  770. if (state == 1) {
  771. //添加
  772. NSString *sfzhm = @"no";
  773. if (myDelegate.isLogin) {
  774. sfzhm = RQ_USER_MANAGER.currentUser._id;
  775. }
  776. //在调这个方法前已经判断过 是否已经存在
  777. 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]];
  778. }
  779. if (state == 0) {
  780. //删除
  781. NSString *sfzhm = @"no";
  782. if (myDelegate.isLogin) {
  783. sfzhm = RQ_USER_MANAGER.currentUser._id;
  784. }
  785. isOk = [shareDataBase executeUpdate:[NSString stringWithFormat:@"delete from markQuestion where _id=%ld and type='%@' and sfzhm='%@'",(long)qId,markType,sfzhm]];
  786. }
  787. [shareDataBase close];
  788. }
  789. return isOk;
  790. }
  791. +(BOOL)queryIsHaveWithQuestion:(NSInteger)qId Type:(NSString *)markType
  792. {
  793. BOOL isOk = NO;
  794. shareDataBase=[DB_Que_Helper createDataBase];
  795. if ([shareDataBase open]) {
  796. NSString *sfzhm = @"no";
  797. if (myDelegate.isLogin) {
  798. sfzhm = RQ_USER_MANAGER.currentUser._id;
  799. }
  800. 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]];
  801. if (cnt > 0) {
  802. isOk = YES;
  803. }
  804. [shareDataBase close];
  805. }
  806. return isOk;
  807. }
  808. +(NSArray *)queryMarkQuestionWithType:(NSString *)markType
  809. {
  810. NSMutableArray *questions = [[NSMutableArray alloc]init];
  811. shareDataBase=[DB_Que_Helper createDataBase];
  812. if ([shareDataBase open]) {
  813. NSString *sfzhm = @"no";
  814. if (myDelegate.isLogin) {
  815. sfzhm = RQ_USER_MANAGER.currentUser._id;
  816. }
  817. NSString *sql;//1、错题 2、收藏 3、排错 4、已做未做 5、除去排除的题
  818. if ([markType isEqualToString:@"4"]){
  819. 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];
  820. }else if ([markType isEqualToString:@"5"]){
  821. //除去排错剩下的所有题
  822. 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];
  823. if ([myDelegate.subject isEqualToString:@"4"])
  824. {
  825. 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];
  826. if ([RQ_YDTQuestion_Module.car_type isEqualToString:@"D"]) {
  827. 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];
  828. }
  829. }
  830. }else{
  831. 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];
  832. }
  833. //如果是资格证 就将科目的判断去掉
  834. 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"])
  835. {
  836. if ([sql containsString:[NSString stringWithFormat:@"subject = '%@' and",myDelegate.subject]]) {
  837. sql = [sql stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"subject = '%@' and",myDelegate.subject] withString:@""];
  838. }
  839. }
  840. FMResultSet *s=[shareDataBase executeQuery:sql];
  841. questions = [Question questionsWithFMResultSet:s];
  842. // NSLog(@"这里找出要显示的题目sql-->%@------------count-->%d",sql,(int)questions.count);
  843. [shareDataBase close];
  844. }
  845. return questions;
  846. }
  847. +(NSArray *)queryMarkQuestionWithUpData:(NSInteger)isUpdata MarkType:(NSString *)markType Subject:(NSString *)sub
  848. {
  849. NSMutableArray<NSString *> *array = [NSMutableArray array];
  850. shareDataBase=[DB_Que_Helper createDataBase];
  851. if ([shareDataBase open]) {
  852. NSString *sfzhm = @"no";
  853. if (myDelegate.isLogin) {
  854. sfzhm = RQ_USER_MANAGER.currentUser._id;
  855. }
  856. NSString *sql;
  857. 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];
  858. if (isUpdata == 1) {
  859. sql = [sql stringByReplacingOccurrencesOfString:@" and isUpdata = '0'" withString:@""];
  860. }
  861. FMResultSet *s=[shareDataBase executeQuery:sql];
  862. while ([s next]) {
  863. NSString *questId = [s stringForColumn:@"_id"];
  864. [array addObject:questId];
  865. }
  866. [shareDataBase close];
  867. }
  868. //NSLog(@"题目---><>%@",array);
  869. return array;
  870. }
  871. + (NSInteger)queryMarkQuestionWithMarkType:(NSString *)markType {
  872. NSInteger cnt = 0;
  873. shareDataBase=[DB_Que_Helper createDataBase];
  874. if ([shareDataBase open]) {
  875. NSString *sfzhm = @"no";
  876. if (myDelegate.isLogin) {
  877. sfzhm = RQ_USER_MANAGER.currentUser._id;
  878. }
  879. 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];
  880. cnt = [shareDataBase intForQuery:queryStr];
  881. [shareDataBase close];
  882. }
  883. return cnt;
  884. }
  885. +(BOOL)setIsUpdateWithType:(NSString *)markType
  886. {
  887. BOOL isOk = NO;
  888. shareDataBase=[DB_Que_Helper createDataBase];
  889. if ([shareDataBase open]) {
  890. NSString *sfzhm = @"no";
  891. if (myDelegate.isLogin) {
  892. sfzhm = RQ_USER_MANAGER.currentUser._id;
  893. }
  894. NSString *sql;
  895. if ([markType isEqualToString:@"0"]) {
  896. sql = [NSString stringWithFormat:@"update markQuestion set isUpdata = '1' where subject = '%@' and car_type = '%@' and sfzhm = '%@'",myDelegate.subject,RQ_YDTQuestion_Module.car_type,sfzhm];
  897. }else{
  898. 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];
  899. }
  900. isOk = [shareDataBase executeUpdate:sql];
  901. [shareDataBase close];
  902. }
  903. return isOk;
  904. }
  905. +(BOOL)setIsUpdateWithArray:(NSArray<NSString *> *)array
  906. {
  907. BOOL isOk = NO;
  908. shareDataBase=[DB_Que_Helper createDataBase];
  909. if ([shareDataBase open]) {
  910. NSString *sfzhm = @"no";
  911. if (myDelegate.isLogin) {
  912. sfzhm = RQ_USER_MANAGER.currentUser._id;
  913. }
  914. NSString *sql;
  915. NSString *sub1 = @"";
  916. NSString *sub4 = @"";
  917. for (NSString *tagStr in array) {
  918. NSInteger tag = [tagStr integerValue];
  919. NSString *typeStr = @"1";
  920. if (tag == 3 || tag == 6) {
  921. typeStr = @"2";
  922. }
  923. if (tag == 4 || tag == 7) {
  924. typeStr = @"3";
  925. }
  926. if (tag < 5) {//科一
  927. sub1 = [sub1 stringByAppendingString:[NSString stringWithFormat:@"%@,",typeStr]];
  928. }else{//科四
  929. sub4 = [sub4 stringByAppendingString:[NSString stringWithFormat:@"%@,",typeStr]];
  930. }
  931. }
  932. if (sub1.length > 0) {
  933. sub1 = [sub1 substringToIndex:sub1.length - 1];
  934. 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];
  935. isOk = [shareDataBase executeUpdate:sql];
  936. }
  937. if (sub4.length > 0) {
  938. sub4 = [sub4 substringToIndex:sub4.length - 1];
  939. 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];
  940. isOk = [shareDataBase executeUpdate:sql];
  941. }
  942. [shareDataBase close];
  943. }
  944. return isOk;
  945. }
  946. +(BOOL)setMarkQuestionWithArray:(NSArray *)questions
  947. {
  948. BOOL isOk = NO;
  949. BOOL allIsOK = YES;
  950. NSString *sfzhm = @"no";
  951. if (myDelegate.isLogin) {
  952. sfzhm = RQ_USER_MANAGER.currentUser._id;
  953. }
  954. //NSLog(@"-----><>%@",questions);
  955. shareDataBase=[DB_Que_Helper createDataBase];
  956. if ([shareDataBase open]) {
  957. for (NSDictionary *obj in questions) {
  958. NSInteger qId = [obj[@"QID"] integerValue];
  959. NSString *kType = obj[@"TYPE"];
  960. NSString *markType;
  961. if ([kType isEqualToString:@"1"]){
  962. //收藏
  963. markType = @"2";
  964. }else if ([kType isEqualToString:@"2"]){
  965. //错题
  966. markType = @"1";
  967. }else{
  968. //排除
  969. markType = @"3";
  970. }
  971. NSString *cartype = RQ_YDTQuestion_Module.car_type;
  972. if ([obj[@"CARTYPE"] length] > 0) {
  973. cartype = obj[@"CARTYPE"];
  974. }
  975. NSString *sub = myDelegate.subject;
  976. if ([obj[@"SUBJECT"] length] > 0) {
  977. sub = obj[@"SUBJECT"];
  978. }
  979. if ([markType isEqualToString:@"1"]) {
  980. 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]];
  981. if (cnt == 0) {
  982. [shareDataBase executeUpdate:[NSString stringWithFormat:@"insert into markQuestion (_id,type,isUpdata,subject,car_type,sfzhm)values(%ld,'4','1','%@','%@','%@')",(long)qId,sub,cartype,sfzhm]];
  983. }
  984. }
  985. //dansonmark这里的科目 车型应该来自服务器 如果服务器不返回 就要从本地题目库查
  986. isOk = [shareDataBase executeUpdate:[NSString stringWithFormat:@"insert into markQuestion (_id,type,isUpdata,subject,car_type,sfzhm)values(%ld,'%@','1','%@','%@','%@')",(long)qId,markType,sub,cartype,sfzhm]];
  987. if (isOk == NO) {
  988. allIsOK = NO;
  989. }
  990. }
  991. [shareDataBase close];
  992. }
  993. return allIsOK;
  994. }
  995. +(BOOL)deleteAllMarkQuestion
  996. {
  997. BOOL isOk = NO;
  998. NSString *sfzhm = @"no";
  999. if (myDelegate.isLogin) {
  1000. sfzhm = RQ_USER_MANAGER.currentUser._id;
  1001. }
  1002. shareDataBase=[DB_Que_Helper createDataBase];
  1003. if ([shareDataBase open]) {
  1004. isOk = [shareDataBase executeUpdate:[NSString stringWithFormat:@"delete from markQuestion where type != '4' and sfzhm='%@'",sfzhm]];
  1005. [shareDataBase close];
  1006. }
  1007. return isOk;
  1008. }
  1009. +(void)moveIsDoneToMarkQuestion
  1010. {
  1011. //等以后就版本没有了 就不用管这个了
  1012. NSArray *columnArray = [DB_Que_Helper queryColumnNameWithTable:@"question"];
  1013. if (![columnArray containsObject:@"done"]) {
  1014. //说明是新版 没有了done字段。不需要转移已做题
  1015. return;
  1016. }
  1017. NSString *sfzhm = @"no";
  1018. if (myDelegate.isLogin) {
  1019. sfzhm = RQ_USER_MANAGER.currentUser._id;
  1020. }
  1021. shareDataBase=[DB_Que_Helper createDataBase];
  1022. if ([shareDataBase open]) {
  1023. FMResultSet *s=[shareDataBase executeQuery:@"select * from question where done = 1"];
  1024. NSMutableArray *questions = [Question questionsWithFMResultSet:s];
  1025. for (Question *question in questions) {
  1026. //NSLog(@"插入已做题");
  1027. [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]];
  1028. }
  1029. [shareDataBase close];
  1030. }
  1031. }
  1032. + (void)updateQuestionWithId:(NSString *)idStr {
  1033. NSString *sqlStr0 = [NSString stringWithFormat:@"UPDATE question SET question = '%@' WHERE _id = %ld",self.updateDataDic[idStr][@"question"],(long)idStr.integerValue];
  1034. NSString *sqlStr1 = [NSString stringWithFormat:@"UPDATE question SET explain = '%@' WHERE _id = %ld",self.updateDataDic[idStr][@"explain"],(long)idStr.integerValue];
  1035. BOOL isSuccess0 = [shareDataBase executeUpdate:sqlStr0];
  1036. BOOL isSuccess1 = [shareDataBase executeUpdate:sqlStr1];
  1037. NSLog(@"%@ %@",isSuccess0? @"Success!" : @"Failed!",isSuccess1? @"Success!" : @"Failed!");
  1038. }
  1039. + (NSDictionary *)updateDataDic {
  1040. return @{
  1041. @"332" : @{
  1042. @"question" : @"年龄在70周岁以上的驾驶人多长时间提交一次身体条件证明?",
  1043. @"explain" : @"新修改的《机动车驾驶证申领和使用规定》还放宽了未体检注销驾驶证后恢复驾驶资格的年龄,规定:年龄在70周岁以上的驾驶人,在一个记分周期结束后一年内未提交身体条件证明的,车辆管理所将注销其机动车驾驶证。",
  1044. },
  1045. @"466" : @{
  1046. @"question" : @"年龄在70周岁以上,在一个记分周期结束后一年内未提交身体条件证明的,其机动车驾驶证将会被车辆管理所注销。",
  1047. @"explain" : @"新修改的《机动车驾驶证申领和使用规定》还放宽了未体检注销驾驶证后恢复驾驶资格的年龄,规定:年龄在70周岁以上的驾驶人,在一个记分周期结束后一年内未提交身体条件证明的,车辆管理所将注销其机动车驾驶证。",
  1048. },
  1049. @"521" : @{
  1050. @"question" : @"年龄在70周岁以上的机动车驾驶人,应当每年进行一次身体检查的目的是什么?",
  1051. @"explain" : @"新修改的《机动车驾驶证申领和使用规定》还放宽了未体检注销驾驶证后恢复驾驶资格的年龄,规定:年龄在70周岁以上的驾驶人,在一个记分周期结束后一年内未提交身体条件证明的,车辆管理所将注销其机动车驾驶证。",
  1052. },
  1053. @"3960" : @{
  1054. @"question" : @"申请小型汽车准驾车型驾驶证的人年龄条件是多少?",
  1055. @"explain" : @"《公安部令第123号》第十一条:申请小型汽车、小型自动挡汽车、残疾人专用小型自动挡载客汽车、轻便摩托车准驾车型的,在18周岁以上、70周岁以下。",
  1056. },
  1057. @"3962" : @{
  1058. @"question" : @"申请小型汽车汽车驾驶证的,年龄应在18周岁以上70周岁以下。",
  1059. @"explain" : @"《公安部令第123号》第十一条:申请小型汽车、小型自动挡汽车、残疾人专用小型自动挡载客汽车、轻便摩托车准驾车型的,在18周岁以上、70周岁以下。",
  1060. },
  1061. @"3976" : @{
  1062. @"question" : @"年龄在70周岁以上的驾驶人多长时间提交一次身体条件证明?",
  1063. @"explain" : @"新修改的《机动车驾驶证申领和使用规定》还放宽了未体检注销驾驶证后恢复驾驶资格的年龄,规定:年龄在70周岁以上的驾驶人,在一个记分周期结束后一年内未提交身体条件证明的,车辆管理所将注销其机动车驾驶证。",
  1064. },
  1065. @"2738" : @{
  1066. @"question" : @"申请小型汽车准驾车型驾驶证的人年龄条件是多少?",
  1067. @"explain" : @"《公安部令第123号》第十一条:申请小型汽车、小型自动挡汽车、残疾人专用小型自动挡载客汽车、轻便摩托车准驾车型的,在18周岁以上、70周岁以下。",
  1068. },
  1069. @"2740" : @{
  1070. @"question" : @"申请小型汽车汽车驾驶证的,年龄应在18周岁以上70周岁以下。",
  1071. @"explain" : @"《公安部令第123号》第十一条:申请小型汽车、小型自动挡汽车、残疾人专用小型自动挡载客汽车、轻便摩托车准驾车型的,在18周岁以上、70周岁以下。",
  1072. },
  1073. @"2754" : @{
  1074. @"question" : @"年龄在70周岁以上的驾驶人多长时间提交一次身体条件证明?",
  1075. @"explain" : @"新修改的《机动车驾驶证申领和使用规定》还放宽了未体检注销驾驶证后恢复驾驶资格的年龄,规定:年龄在70周岁以上的驾驶人,在一个记分周期结束后一年内未提交身体条件证明的,车辆管理所将注销其机动车驾驶证。",
  1076. },
  1077. @"3627" : @{
  1078. @"question" : @"年龄在70周岁以上,在1个记分周期结束后,( )内未提交身体检查结果的,车辆管理所注销其驾驶证。",
  1079. @"explain" : @"新修改的《机动车驾驶证申领和使用规定》还放宽了未体检注销驾驶证后恢复驾驶资格的年龄,规定:年龄在70周岁以上的驾驶人,在一个记分周期结束后一年内未提交身体条件证明的,车辆管理所将注销其机动车驾驶证。",
  1080. },
  1081. @"3631" : @{
  1082. @"question" : @"年龄在60岁以上,超过机动车驾驶证有效期一年以上未换证的,车辆管理所将注销其驾驶证。",
  1083. @"explain" : @"《公安部令第123号》第六十七条:机动车驾驶人具有下列情形之一的,车辆管理所应当注销其机动车驾驶证:(七)超过机动车驾驶证有效期一年以上未换证的。",
  1084. },
  1085. @"3595" : @{
  1086. @"question" : @"年龄在60岁以上持有准驾车型为大型客车的驾驶人,应当到驾驶证核发地车辆管理所换领准驾车型为小型汽车或小型自动挡汽车的驾驶证。",
  1087. @"explain" : @"《公安部令第123号》第五十条:年龄在60周岁以上的,不得驾驶大型客车、牵引车、城市公交车、中型客车、大型货车、无轨电车和有轨电车;持有大型客车、牵引车、城市公交车、中型客车、大型货车驾驶证的,应当到机动车驾驶证核发地车辆管理所换领准驾车型为小型汽车或者小型自动挡汽车的机动车驾驶证。",
  1088. },
  1089. };
  1090. }
  1091. + (NSArray *)updateIdArr {
  1092. return @[@"332", @"466", @"521", @"3960", @"3962", @"3976", @"2738", @"2740", @"2754", @"3627", @"3631", @"3595"];
  1093. }
  1094. @end