DB_Helper.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. //
  2. // DB_Helper.m
  3. // Texts
  4. //
  5. // Created by tongjun on 13-10-12.
  6. // Copyright (c) 2013年 tongjun. All rights reserved.
  7. //
  8. #import "DB_Helper.h"
  9. #import "Question.h"
  10. #import "DES3Util.h"
  11. static FMDatabase *shareDataBase = nil;
  12. @implementation DB_Helper
  13. /**
  14. 创建数据库类的单例对象
  15. */
  16. + (FMDatabase *)createDataBase {
  17. if ([[NSFileManager defaultManager] fileExistsAtPath:dataBasePath]) {
  18. }else{
  19. [DB_Helper copyFileDatabase];
  20. }
  21. //transrules.db 这个库
  22. static dispatch_once_t onceToken;
  23. dispatch_once(&onceToken, ^{
  24. shareDataBase = [FMDatabase databaseWithPath:dataBasePath];
  25. if ([shareDataBase open]) {
  26. //本地成绩表
  27. [shareDataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS grades (km text, score text, startDate text, endDate text, state text, isUpload text, subject text);"];
  28. //实操学时表
  29. [shareDataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS scPeriod_record (userId text, classId text, classCode text, trainNum text, maxSpeed text, mileage text, gps text, obdRotationSpeed text, obdMileage text);"];
  30. //判断字段是否存在 老学员的表中没有这个字段 这里判断添加 并赋初始值
  31. if (![shareDataBase columnExists:@"obdRotationSpeed" inTableWithName:@"scPeriod_record"]){
  32. //这个方法第一次用必进的
  33. [shareDataBase executeUpdate:[NSString stringWithFormat:@"ALTER TABLE scPeriod_record ADD obdRotationSpeed text"]];
  34. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update scPeriod_record set obdRotationSpeed = '0'"]];
  35. }
  36. if (![shareDataBase columnExists:@"obdMileage" inTableWithName:@"scPeriod_record"]){
  37. //这个方法第一次用必进的
  38. [shareDataBase executeUpdate:[NSString stringWithFormat:@"ALTER TABLE scPeriod_record ADD obdMileage text"]];
  39. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update scPeriod_record set obdMileage = '0'"]];
  40. }
  41. //这个是不用写的 理论学时表 数据库有
  42. [shareDataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS train_record (student_id text, begin_time text, end_time text, train_time text, state text, subject text);"];
  43. if (![shareDataBase columnExists:@"subject" inTableWithName:@"train_record"]){
  44. //这个方法第一次用必进的
  45. [shareDataBase executeUpdate:[NSString stringWithFormat:@"ALTER TABLE train_record ADD subject text"]];
  46. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update train_record set subject = '1'"]];
  47. }
  48. }
  49. [shareDataBase close];
  50. });
  51. return shareDataBase;
  52. }
  53. /**
  54. 关闭数据库
  55. **/
  56. + (void)closeDataBase {
  57. if(![shareDataBase close]) {
  58. NSLog(@"数据库关闭异常,请检查");
  59. return;
  60. }
  61. }
  62. //拷贝数据库
  63. +(void)copyFileDatabase {
  64. NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask, YES);
  65. NSString *documentsDirectory = [paths objectAtIndex:0];
  66. NSString *documentLibraryFolderPath = [documentsDirectory stringByAppendingPathComponent:dataBaseName];
  67. if ([[NSFileManager defaultManager] fileExistsAtPath:documentLibraryFolderPath]) {
  68. // NSLog(@"文件已经存在了");
  69. }else {
  70. NSString *resourceSampleImagesFolderPath =[[NSBundle mainBundle]pathForResource:@"transrules" ofType:@"db"];
  71. NSData *mainBundleFile = [NSData dataWithContentsOfFile:resourceSampleImagesFolderPath];
  72. [[NSFileManager defaultManager] createFileAtPath:documentLibraryFolderPath
  73. contents:mainBundleFile
  74. attributes:nil];
  75. }
  76. }
  77. #pragma mark 理论题目练习成绩表
  78. /**创建grade表。
  79. (sfzmhm text, km text , score text,startDate text,endDate text,subject text)
  80. */
  81. +(void)saveGrade:(Grade*)aGrade
  82. {
  83. if (!aGrade) {
  84. aGrade = [Grade new];
  85. }
  86. // NSLog(@"graScore ->%@",aGrade.graScore);
  87. if ([shareDataBase open]) {
  88. [shareDataBase executeUpdate:[NSString stringWithFormat:@"insert into grades (km,score,startDate,endDate,state,isUpload)values('%@','%@','%@','%@','0','%@')",myDelegate.subject,aGrade.graScore,aGrade.startDate,aGrade.endDate,aGrade.isUpdate]];
  89. }
  90. // NSLog(@"savePath->%@",dataBasePath);
  91. [shareDataBase close];
  92. }
  93. +(NSArray *)quearyGrade
  94. {
  95. NSMutableArray *trains=[[NSMutableArray alloc]init];
  96. shareDataBase=[DB_Helper createDataBase];
  97. if([shareDataBase open]){
  98. FMResultSet *s=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from grades where state = '0' and isUpload = '1'"]];
  99. while([s next]){
  100. Grade *train=[[Grade alloc]init];
  101. train.subject=[s stringForColumn:@"km"];
  102. train.startDate=[s stringForColumn:@"startDate"];
  103. train.endDate=[s stringForColumn:@"endDate"];
  104. train.graScore=[s stringForColumn:@"score"];
  105. [trains addObject:train];
  106. }
  107. }
  108. return trains;
  109. }
  110. +(void)updateGrade:(Grade *)aGrade{
  111. shareDataBase=[DB_Helper createDataBase];
  112. if([shareDataBase open]){
  113. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update grades set state='1' where startDate='%@'",aGrade.startDate]];
  114. [shareDataBase close];
  115. }
  116. }
  117. #pragma mark 理论学时表
  118. /**
  119. 貌似不需要sfzmhm。如果用户还未登录是没有这个sfzmhm的。
  120. 在上传时,只需要搜索state为0的记录即可 全部上传。
  121. */
  122. +(void)saveTrain:(TrainRecord *)trainRecord
  123. {
  124. shareDataBase=[DB_Helper createDataBase];
  125. if ([shareDataBase open]) {
  126. //此查询有开始时间 不必用科目再查
  127. FMResultSet *s = [shareDataBase executeQuery:[NSString stringWithFormat:@"SELECT count(*) FROM train_record WHERE begin_time='%@' and student_id=%@",[DES3Util encrypt:trainRecord.beginTime],trainRecord.studentId]];
  128. int count=0;
  129. if ([s next]) {
  130. count=[[s stringForColumnIndex:0]intValue];
  131. }
  132. if (count==0) {
  133. NSString *pxkm = defUser.userDict[@"pxjd"];
  134. if (pxkm.length < 1) {
  135. pxkm = @"1";
  136. }
  137. // if ([pxkm isEqualToString:@"3"]) {
  138. // pxkm = @"4";
  139. // }
  140. if ([defUser.userDict[@"city"] isEqualToString:@"3501"] && [pxkm isEqualToString:@"3"]) {
  141. /// 福州学员在科目三阶段时可以打科目四学时
  142. pxkm = @"4";
  143. } else if ([defUser.userDict[@"city"] isEqualToString:@"3502"] && [pxkm isEqualToString:@"3"]) {
  144. /// 厦门学员在科目三阶段时可以打科目四学时
  145. pxkm = @"4";
  146. } else if ([defUser.userDict[@"city"] isEqualToString:@"3503"] && ([pxkm isEqualToString:@"2"] || [pxkm isEqualToString:@"3"])) {
  147. /// 莆田学员在科目二科目三阶段时可以打科目四学时
  148. pxkm = @"4";
  149. }
  150. [shareDataBase executeUpdate:[NSString stringWithFormat:@"insert into train_record (student_id,begin_time,end_time,train_time,subject)values('%@','%@','%@','%@','%@')",trainRecord.studentId,[DES3Util encrypt:trainRecord.beginTime],[DES3Util encrypt:trainRecord.endTime],[DES3Util encrypt:trainRecord.trainTime],pxkm]];
  151. } else{
  152. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update train_record set end_time='%@',train_time='%@' where begin_time='%@' and student_id=%@",[DES3Util encrypt:trainRecord.endTime],[DES3Util encrypt:trainRecord.trainTime],[DES3Util encrypt:trainRecord.beginTime],trainRecord.studentId]];
  153. }
  154. [shareDataBase close];
  155. }
  156. }
  157. +(NSArray *)quearyTrain:(NSString *)stuId Subject:(NSString *)sub{
  158. //NSLog(@"quearyTrain--sub----><>%@",sub);
  159. NSMutableArray *trains=[[NSMutableArray alloc]init];
  160. shareDataBase=[DB_Helper createDataBase];
  161. if([shareDataBase open]){
  162. FMResultSet *s=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from train_record where state=0 and student_id=%@ and subject = '%@'",stuId,sub]];
  163. while([s next]){
  164. TrainRecord *train = [[TrainRecord alloc]init];
  165. train.studentId = [s stringForColumn:@"student_id"];
  166. train.beginTime = [DES3Util decrypt:[s stringForColumn:@"begin_time"]];
  167. train.endTime = [DES3Util decrypt:[s stringForColumn:@"end_time"]];
  168. train.state = [s stringForColumn:@"state"];
  169. train.trainTime = [DES3Util decrypt:[s stringForColumn:@"train_time"]];
  170. train.subject = [s stringForColumn:@"subject"];
  171. [trains addObject:train];
  172. }
  173. [shareDataBase close];
  174. }
  175. return trains;
  176. }
  177. +(void)updateTrainState:(TrainRecord *)trainRecord{
  178. shareDataBase=[DB_Helper createDataBase];
  179. if([shareDataBase open]){
  180. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update train_record set state=1 where begin_time='%@' and student_id=%@",[DES3Util encrypt:trainRecord.beginTime],trainRecord.studentId]];
  181. [shareDataBase close];
  182. }
  183. }
  184. #pragma mark 实操计时表
  185. //scPeriod_record (userId text, classId text, classCode text, trainNum text, maxSpeed text, mileage text, gps text, obdRotationSpeed text, obdMileage text)
  186. + (void)saveSCTrainWithDic:(NSDictionary *)dic
  187. {
  188. shareDataBase=[DB_Helper createDataBase];
  189. if ([shareDataBase open]) {
  190. [shareDataBase executeUpdate:[NSString stringWithFormat:@"insert into scPeriod_record (userId,classId,classCode,trainNum,maxSpeed,mileage,gps,obdRotationSpeed,obdMileage)values(%@,'%@','%@','%@','%@','%@','%@','0','0')",defUser.userDict[@"id"],dic[@"classId"],[DES3Util encrypt:dic[@"classCode"]],[DES3Util encrypt:dic[@"trainNum"]],dic[@"maxSpeed"],dic[@"mileage"],dic[@"gps"]]];
  191. [shareDataBase close];
  192. }
  193. }
  194. + (void)deleteSCTrainWithTrainNum:(NSString *)trainNum
  195. {
  196. shareDataBase=[DB_Helper createDataBase];
  197. if ([shareDataBase open]) {
  198. [shareDataBase executeUpdate:[NSString stringWithFormat:@"delete from scPeriod_record where userId = '%@' and trainNum = '%@'",defUser.userDict[@"id"],[DES3Util encrypt:trainNum]]];
  199. [shareDataBase close];
  200. }
  201. }
  202. + (void)deleteSCTrainWithClassId:(NSString *)classId
  203. {
  204. shareDataBase=[DB_Helper createDataBase];
  205. if ([shareDataBase open]) {
  206. [shareDataBase executeUpdate:[NSString stringWithFormat:@"delete from scPeriod_record where userId = '%@' and classId = '%@'",defUser.userDict[@"id"],classId]];
  207. [shareDataBase close];
  208. }
  209. }
  210. +(void)deleteAllSCTrain
  211. {
  212. shareDataBase=[DB_Helper createDataBase];
  213. if ([shareDataBase open]) {
  214. [shareDataBase executeUpdate:[NSString stringWithFormat:@"delete from scPeriod_record where userId = '%@'",defUser.userDict[@"id"]]];
  215. [shareDataBase close];
  216. }
  217. }
  218. + (NSArray *)quearySCTrainWithClassId:(NSString *)classId
  219. {
  220. NSMutableArray *scTrains = [[NSMutableArray alloc]init];
  221. shareDataBase = [DB_Helper createDataBase];
  222. if([shareDataBase open]){
  223. FMResultSet *s = [shareDataBase executeQuery:[NSString stringWithFormat:@"select * from scPeriod_record where userId = '%@' and classId = '%@'",defUser.userDict[@"id"],classId]];
  224. while([s next]){
  225. //classId , classCode , trainNum , maxSpeed , mileage , gps
  226. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  227. [dic setValue:[s stringForColumn:@"classId"] forKey:@"classId"];
  228. [dic setValue:[s stringForColumn:@"maxSpeed"] forKey:@"maxSpeed"];
  229. [dic setValue:[s stringForColumn:@"mileage"] forKey:@"mileage"];
  230. [dic setValue:[s stringForColumn:@"gps"] forKey:@"gps"];
  231. [dic setValue:[s stringForColumn:@"obdRotationSpeed"] forKey:@"obdRotationSpeed"];
  232. [dic setValue:[s stringForColumn:@"obdMileage"] forKey:@"obdMileage"];
  233. [dic setValue:[DES3Util decrypt:[s stringForColumn:@"classCode"]] forKey:@"classCode"];
  234. [dic setValue:[DES3Util decrypt:[s stringForColumn:@"trainNum"]] forKey:@"trainNum"];
  235. [scTrains addObject:dic];
  236. }
  237. [shareDataBase close];
  238. }
  239. return scTrains;
  240. }
  241. + (void)changeSCTrainWithTrainNum:(NSString *)trainNum
  242. RotationSpeed:(NSString *)rotationSpeed
  243. Speed:(NSString *)speed
  244. Mileage:(NSString *)mileage{
  245. shareDataBase=[DB_Helper createDataBase];
  246. if([shareDataBase open]){
  247. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update scPeriod_record set obdRotationSpeed='%@' and maxSpeed='%@' and obdMileage='%@' where trainNum='%@'",rotationSpeed,speed,mileage,[DES3Util encrypt:trainNum]]];
  248. [shareDataBase close];
  249. }
  250. }
  251. #pragma mark 实操/模拟/大车计时表
  252. //scPeriod_record (userId text, classId text, classCode text, trainNum text, maxSpeed text, mileage text, gps text)
  253. + (void)saveTrainWithDic:(NSDictionary *)dic type:(NSString *)type
  254. {
  255. shareDataBase=[DB_Helper createDataBase];
  256. if ([shareDataBase open]) {
  257. NSString *sql= [NSString stringWithFormat:@"insert into %@_record (userId,classId,classCode,trainNum,maxSpeed,mileage,gps)values(%@,'%@','%@','%@','%@','%@','%@')",type,defUser.userDict[@"id"],dic[@"classId"],[DES3Util encrypt:dic[@"classCode"]],[DES3Util encrypt:dic[@"trainNum"]],dic[@"maxSpeed"],dic[@"mileage"],dic[@"gps"]];
  258. BOOL zz = [shareDataBase executeUpdate:sql];
  259. if (zz) {
  260. NSLog(@"写入成功");
  261. }else{
  262. NSLog(@"写入失败");
  263. }
  264. [shareDataBase close];
  265. }
  266. }
  267. + (void)deleteTrainWithTrainNum:(NSString *)trainNum type:(NSString *)type
  268. {
  269. shareDataBase=[DB_Helper createDataBase];
  270. if ([shareDataBase open]) {
  271. [shareDataBase executeUpdate:[NSString stringWithFormat:@"delete from %@_record where userId = '%@' and trainNum = '%@'",type,defUser.userDict[@"id"],[DES3Util encrypt:trainNum]]];
  272. [shareDataBase close];
  273. }
  274. }
  275. + (void)deleteTrainWithClassId:(NSString *)classId type:(NSString *)type
  276. {
  277. shareDataBase=[DB_Helper createDataBase];
  278. if ([shareDataBase open]) {
  279. [shareDataBase executeUpdate:[NSString stringWithFormat:@"delete from %@_record where userId = '%@' and classId = '%@'",type,defUser.userDict[@"id"],classId]];
  280. [shareDataBase close];
  281. }
  282. }
  283. +(void)deleteAllTrainWithType:(NSString *)type
  284. {
  285. shareDataBase=[DB_Helper createDataBase];
  286. if ([shareDataBase open]) {
  287. [shareDataBase executeUpdate:[NSString stringWithFormat:@"delete from %@_record where userId = '%@'",type,defUser.userDict[@"id"]]];
  288. [shareDataBase close];
  289. }
  290. }
  291. + (NSArray *)quearyTrainWithClassId:(NSString *)classId type:(NSString *)type
  292. {
  293. NSMutableArray *scTrains = [[NSMutableArray alloc]init];
  294. shareDataBase = [DB_Helper createDataBase];
  295. if([shareDataBase open]){
  296. FMResultSet *s = [shareDataBase executeQuery:[NSString stringWithFormat:@"select * from %@_record where userId = '%@' and classId = '%@'",type,defUser.userDict[@"id"],classId]];
  297. NSLog(@"%@ \n FMResultSet_colounm: %d",s.query,s.columnCount);
  298. while([s next]){
  299. //classId , classCode , trainNum , maxSpeed , mileage , gps
  300. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  301. [dic setValue:[s stringForColumn:@"classId"] forKey:@"classId"];
  302. [dic setValue:[s stringForColumn:@"maxSpeed"] forKey:@"maxSpeed"];
  303. [dic setValue:[s stringForColumn:@"mileage"] forKey:@"mileage"];
  304. [dic setValue:[s stringForColumn:@"gps"] forKey:@"gps"];
  305. [dic setValue:[DES3Util decrypt:[s stringForColumn:@"classCode"]] forKey:@"classCode"];
  306. [dic setValue:[DES3Util decrypt:[s stringForColumn:@"trainNum"]] forKey:@"trainNum"];
  307. [scTrains addObject:dic];
  308. }
  309. [shareDataBase close];
  310. }
  311. return scTrains;
  312. }
  313. @end