DB_Helper.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. NSLog(@"%@",dataBasePath);
  18. if ([[NSFileManager defaultManager] fileExistsAtPath:dataBasePath]) {
  19. }else{
  20. [DB_Helper copyFileDatabase];
  21. }
  22. //transrules.db 这个库
  23. static dispatch_once_t onceToken;
  24. dispatch_once(&onceToken, ^{
  25. shareDataBase = [FMDatabase databaseWithPath:dataBasePath];
  26. if ([shareDataBase open]) {
  27. //本地成绩表
  28. [shareDataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS grades (km text, score text, startDate text, endDate text, state text, isUpload text, subject text);"];
  29. //实操学时表
  30. [shareDataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS scPeriod_record (userId text, classId text, classCode text, trainNum text, maxSpeed text, mileage text, gps text);"];
  31. //模拟学时表
  32. [shareDataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS mnPeriod_record (userId text, classId text, classCode text, trainNum text, maxSpeed text, mileage text, gps text);"];
  33. //大车学时表
  34. [shareDataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS dcPeriod_record (userId text, classId text, classCode text, trainNum text, maxSpeed text, mileage text, gps text);"];
  35. //这个是不用写的 理论学时表 数据库有
  36. [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, classid text);"];
  37. if (![shareDataBase columnExists:@"subject" inTableWithName:@"train_record"]){
  38. //这个方法第一次用必进的
  39. [shareDataBase executeUpdate:[NSString stringWithFormat:@"ALTER TABLE train_record ADD subject text"]];
  40. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update train_record set subject = '1'"]];
  41. }
  42. if (![shareDataBase columnExists:@"classid" inTableWithName:@"train_record"]){
  43. //这个方法第一次用必进的
  44. [shareDataBase executeUpdate:[NSString stringWithFormat:@"ALTER TABLE train_record ADD classid text"]];
  45. }
  46. }
  47. [shareDataBase close];
  48. });
  49. return shareDataBase;
  50. }
  51. /**
  52. 关闭数据库
  53. **/
  54. + (void)closeDataBase {
  55. if(![shareDataBase close]) {
  56. NSLog(@"数据库关闭异常,请检查");
  57. return;
  58. }
  59. }
  60. //拷贝数据库
  61. +(void)copyFileDatabase {
  62. NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask, YES);
  63. NSString *documentsDirectory = [paths objectAtIndex:0];
  64. NSString *documentLibraryFolderPath = [documentsDirectory stringByAppendingPathComponent:dataBaseName];
  65. if ([[NSFileManager defaultManager] fileExistsAtPath:documentLibraryFolderPath]) {
  66. // NSLog(@"文件已经存在了");
  67. }else {
  68. NSString *resourceSampleImagesFolderPath =[[NSBundle mainBundle]pathForResource:@"transrules" ofType:@"db"];
  69. NSData *mainBundleFile = [NSData dataWithContentsOfFile:resourceSampleImagesFolderPath];
  70. [[NSFileManager defaultManager] createFileAtPath:documentLibraryFolderPath
  71. contents:mainBundleFile
  72. attributes:nil];
  73. }
  74. }
  75. #pragma mark 理论题目练习成绩表
  76. /**创建grade表。
  77. (sfzmhm text, km text , score text,startDate text,endDate text,subject text)
  78. */
  79. +(void)saveGrade:(Grade*)aGrade
  80. {
  81. if (!aGrade) {
  82. aGrade = [Grade new];
  83. }
  84. // NSLog(@"graScore ->%@",aGrade.graScore);
  85. if ([shareDataBase open]) {
  86. [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]];
  87. }
  88. // NSLog(@"savePath->%@",dataBasePath);
  89. [shareDataBase close];
  90. }
  91. +(NSArray *)quearyGrade
  92. {
  93. NSMutableArray *trains=[[NSMutableArray alloc]init];
  94. shareDataBase=[DB_Helper createDataBase];
  95. if([shareDataBase open]){
  96. FMResultSet *s=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from grades where state = '0' and isUpload = '1'"]];
  97. while([s next]){
  98. Grade *train=[[Grade alloc]init];
  99. train.subject=[s stringForColumn:@"km"];
  100. train.startDate=[s stringForColumn:@"startDate"];
  101. train.endDate=[s stringForColumn:@"endDate"];
  102. train.graScore=[s stringForColumn:@"score"];
  103. [trains addObject:train];
  104. }
  105. }
  106. return trains;
  107. }
  108. +(void)updateGrade:(Grade *)aGrade{
  109. shareDataBase=[DB_Helper createDataBase];
  110. if([shareDataBase open]){
  111. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update grades set state='1' where startDate='%@'",aGrade.startDate]];
  112. [shareDataBase close];
  113. }
  114. }
  115. #pragma mark 理论学时表
  116. /**
  117. 貌似不需要sfzmhm。如果用户还未登录是没有这个sfzmhm的。
  118. 在上传时,只需要搜索state为0的记录即可 全部上传。
  119. */
  120. +(void)saveTrain:(TrainRecord *)trainRecord
  121. {
  122. shareDataBase=[DB_Helper createDataBase];
  123. if ([shareDataBase open]) {
  124. //此查询有开始时间 不必用科目再查
  125. FMResultSet *s = [shareDataBase executeQuery:[NSString stringWithFormat:@"SELECT count(*) FROM train_record WHERE begin_time='%@' and student_id=%@",[DES3Util encrypt:trainRecord.beginTime],trainRecord.studentId]];
  126. int count=0;
  127. if ([s next]) {
  128. count=[[s stringForColumnIndex:0]intValue];
  129. }
  130. if (count==0) {
  131. // NSString *pxkm = defUser.userDict[@"pxjd"];
  132. //
  133. // if (pxkm.length < 1) {
  134. // pxkm = @"1";
  135. // }
  136. [shareDataBase executeUpdate:[NSString stringWithFormat:@"insert into train_record (student_id,begin_time,end_time,train_time,subject,classid)values('%@','%@','%@','%@','%@', '%@')",trainRecord.studentId,[DES3Util encrypt:trainRecord.beginTime],[DES3Util encrypt:trainRecord.endTime],[DES3Util encrypt:trainRecord.trainTime],trainRecord.subject,trainRecord.classid]];
  137. } else{
  138. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update train_record set end_time='%@',train_time='%@' where begin_time='%@' and student_id=%@ and classid=%@",[DES3Util encrypt:trainRecord.endTime],[DES3Util encrypt:trainRecord.trainTime],[DES3Util encrypt:trainRecord.beginTime],trainRecord.studentId, trainRecord.classid]];
  139. }
  140. [shareDataBase close];
  141. }
  142. }
  143. +(NSArray *)quearyTrain:(NSString *)stuId Subject:(NSString *)sub{
  144. //NSLog(@"quearyTrain--sub----><>%@",sub);
  145. NSMutableArray *trains=[[NSMutableArray alloc]init];
  146. shareDataBase=[DB_Helper createDataBase];
  147. if([shareDataBase open]){
  148. FMResultSet *s=[shareDataBase executeQuery:[NSString stringWithFormat:@"select * from train_record where state=0 and student_id=%@ and subject = '%@'",stuId,sub]];
  149. while([s next]){
  150. TrainRecord *train = [[TrainRecord alloc]init];
  151. train.studentId = [s stringForColumn:@"student_id"];
  152. train.beginTime = [DES3Util decrypt:[s stringForColumn:@"begin_time"]];
  153. train.endTime = [DES3Util decrypt:[s stringForColumn:@"end_time"]];
  154. train.state = [s stringForColumn:@"state"];
  155. train.trainTime = [DES3Util decrypt:[s stringForColumn:@"train_time"]];
  156. train.subject = [s stringForColumn:@"subject"];
  157. train.classid = [s stringForColumn:@"classid"];
  158. [trains addObject:train];
  159. }
  160. [shareDataBase close];
  161. }
  162. return trains;
  163. }
  164. +(void)updateTrainState:(TrainRecord *)trainRecord{
  165. shareDataBase=[DB_Helper createDataBase];
  166. if([shareDataBase open]){
  167. [shareDataBase executeUpdate:[NSString stringWithFormat:@"update train_record set state=1 where begin_time='%@' and student_id=%@",[DES3Util encrypt:trainRecord.beginTime],trainRecord.studentId]];
  168. [shareDataBase close];
  169. }
  170. }
  171. + (void)deleteTrain:(TrainRecord *)trainRecord {
  172. shareDataBase = [DB_Helper createDataBase];
  173. if([shareDataBase open]){
  174. [shareDataBase executeUpdate:[NSString stringWithFormat:@"delete from train_record where student_id = '%@' and begin_time = '%@' and classid = '%@'",trainRecord.studentId,[DES3Util encrypt:trainRecord.beginTime],trainRecord.classid]];
  175. [shareDataBase close];
  176. }
  177. }
  178. #pragma mark 实操/模拟/大车计时表
  179. //scPeriod_record (userId text, classId text, classCode text, trainNum text, maxSpeed text, mileage text, gps text)
  180. + (void)saveTrainWithDic:(NSDictionary *)dic type:(NSString *)type
  181. {
  182. shareDataBase=[DB_Helper createDataBase];
  183. if ([shareDataBase open]) {
  184. 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"]];
  185. BOOL zz = [shareDataBase executeUpdate:sql];
  186. if (zz) {
  187. NSLog(@"写入成功");
  188. }else{
  189. NSLog(@"写入失败");
  190. }
  191. [shareDataBase close];
  192. }
  193. }
  194. + (void)deleteTrainWithTrainNum:(NSString *)trainNum type:(NSString *)type
  195. {
  196. shareDataBase=[DB_Helper createDataBase];
  197. if ([shareDataBase open]) {
  198. [shareDataBase executeUpdate:[NSString stringWithFormat:@"delete from %@_record where userId = '%@' and trainNum = '%@'",type,defUser.userDict[@"id"],[DES3Util encrypt:trainNum]]];
  199. [shareDataBase close];
  200. }
  201. }
  202. + (void)deleteTrainWithClassId:(NSString *)classId type:(NSString *)type
  203. {
  204. shareDataBase=[DB_Helper createDataBase];
  205. if ([shareDataBase open]) {
  206. [shareDataBase executeUpdate:[NSString stringWithFormat:@"delete from %@_record where userId = '%@' and classId = '%@'",type,defUser.userDict[@"id"],classId]];
  207. [shareDataBase close];
  208. }
  209. }
  210. +(void)deleteAllTrainWithType:(NSString *)type
  211. {
  212. shareDataBase=[DB_Helper createDataBase];
  213. if ([shareDataBase open]) {
  214. [shareDataBase executeUpdate:[NSString stringWithFormat:@"delete from %@_record where userId = '%@'",type,defUser.userDict[@"id"]]];
  215. [shareDataBase close];
  216. }
  217. }
  218. + (NSArray *)quearyTrainWithClassId:(NSString *)classId type:(NSString *)type
  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 %@_record where userId = '%@' and classId = '%@'",type,defUser.userDict[@"id"],classId]];
  224. NSLog(@"%@ \n FMResultSet_colounm: %d",s.query,s.columnCount);
  225. while([s next]){
  226. //classId , classCode , trainNum , maxSpeed , mileage , gps
  227. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  228. [dic setValue:[s stringForColumn:@"classId"] forKey:@"classId"];
  229. [dic setValue:[s stringForColumn:@"maxSpeed"] forKey:@"maxSpeed"];
  230. [dic setValue:[s stringForColumn:@"mileage"] forKey:@"mileage"];
  231. [dic setValue:[s stringForColumn:@"gps"] forKey:@"gps"];
  232. [dic setValue:[DES3Util decrypt:[s stringForColumn:@"classCode"]] forKey:@"classCode"];
  233. [dic setValue:[DES3Util decrypt:[s stringForColumn:@"trainNum"]] forKey:@"trainNum"];
  234. [scTrains addObject:dic];
  235. }
  236. [shareDataBase close];
  237. }
  238. return scTrains;
  239. }
  240. + (void)deleteTrainRecord:(TrainRecord *)trainRecord {
  241. shareDataBase = [DB_Helper createDataBase];
  242. if([shareDataBase open]){
  243. [shareDataBase executeUpdate:[NSString stringWithFormat:@"delete from train_record where end_time='%@'and train_time='%@' and begin_time='%@' and student_id=%@ and classid=%@",[DES3Util encrypt:trainRecord.endTime],[DES3Util encrypt:trainRecord.trainTime],[DES3Util encrypt:trainRecord.beginTime],trainRecord.studentId, trainRecord.classid]];
  244. [shareDataBase close];
  245. }
  246. }
  247. @end