DB_Que_Helper.m 51 KB

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