/
database.cpp
1045 lines (970 loc) · 36.3 KB
/
database.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <QtGui/QMessageBox>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
/*下面这个头文件很重要,用到这个类QVariant时要添加头文件*/
#include<QtCore/QVariant>
#include <QtCore/QDateTime>
#include <QtCore/QString>
#include <QtDebug>
#include "database.h"
Database::Database()
{
}
Database::~Database()
{
}
bool Database::createConnection(const QString &DatabaseName, const QString &DatabaseFileName)
{
QSqlDatabase db = QSqlDatabase::addDatabase(DatabaseName);
/*if setDatabaseName is ":memory:",the database will be created in the memory
So we set the databaseName is *.db,and the database will be created in the ROM
*/
db.setDatabaseName(DatabaseFileName);
if (!db.open())
{
QMessageBox::critical(0, QObject::tr("数据库错误"), QObject::tr("退出请按Cancel键"),
QMessageBox::Cancel, QMessageBox::NoButton);
return false;
}
return true;
}
bool Database::initFireHis()
{
/*To obtain the default object of QSqlDatabase*/
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
/*List all the tables in the database*/
QStringList tableList = db.tables();
/*if this table does not exist we will create*/
if (!tableList.contains("firehis"))
{
bool success = query.exec("create table firehis (id int primary key, "
"type int,"
"infoid int,"
"geo_addr int,"
"time datetime,"
"str varchar(48))");
if (success)
{
qDebug() <<QObject::tr("firehis successfully created!") ;
//writeFireHis();
}
else
{
qDebug() <<QObject::tr("firehis unsuccessfully created!");
return false;
}
}
return true;
}
void Database::writeFireHis()
{
int host = 1;
int iobr = 1;
int loop = 3;
int node = 16;
int infoid = (host<<24) | (iobr<<16) | (loop<<8) | node;
int geo_addr = (host<<24) | (iobr<<16) | (loop<<8) | node;
QDateTime dt = QDateTime::currentDateTime();
/*Before the opening of each transaction,adding this code to improve the speed*/
QSqlDatabase::database().transaction();
QSqlQuery query;
query.prepare("INSERT INTO firehis(id, type, infoid, geo_addr, time, str)"
"VALUES (?, ?, ?, ?, ?, ?)");
for (int i = 0;i<100; i++)
{
query.addBindValue(i);
query.addBindValue(0x000B4000);
query.addBindValue(infoid);
query.addBindValue(geo_addr);
query.addBindValue(dt);
query.addBindValue(QString("华中科技大学南一楼%1室").arg(i));
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("insert firehis error"));
}
}
/*Close the transaction*/
QSqlDatabase::database().commit();
}
bool Database::initHostInfo()
{
/*To obtain the default object of QSqlDatabase*/
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
/*List all the tables in the database*/
QStringList tableList = db.tables();
/*if this table does not exist we will create*/
if (!tableList.contains("hostset"))
{
bool success = query.exec("create table hostset (id int primary key, "
"hostnum int,"
"systemtype int,"
"lockkeytime int,"
"sound_alarm_state int,"
"gasfirecontrolstate int)");
if (success)
{
qDebug() <<QObject::tr("hostset successfully created!") ;
writeHostInfo();
}
else
{
qDebug() <<QObject::tr("hostset unsuccessfully created!");
return false;
}
}
return true;
}
void Database::writeHostInfo()
{
/*Before the opening of each transaction,adding this code to improve the speed*/
QSqlDatabase::database().transaction();
QSqlQuery query;
query.prepare("INSERT INTO hostset(id,hostnum, systemtype, lockkeytime,sound_alarm_state,gasfirecontrolstate)"
"VALUES (?, ?, ?,?,?,?)");
query.addBindValue(1);
query.addBindValue(1);
query.addBindValue(0);
query.addBindValue(0);
query.addBindValue(0);
query.addBindValue(0);
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("insert hostset error"));
}
/*Close the transaction*/
QSqlDatabase::database().commit();
}
bool Database::initFaultHis()
{
/*To obtain the default object of QSqlDatabase*/
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
/*List all the tables in the database*/
QStringList tableList = db.tables();
/*if this table does not exist we will create*/
if (!tableList.contains("faulthis"))
{
bool success = query.exec("create table faulthis (id int primary key, "
"type int,"
"infoid int,"
"geo_addr int,"
"time datetime,"
"str varchar(48))");
if (success)
{
qDebug() <<QObject::tr("faulthis successfully created!") ;
//writeFaultHis();
}
else
{
qDebug() <<QObject::tr("faulthis unsuccessfully created!");
return false;
}
}
return true;
}
bool Database::initFeedbackHis()
{
/*To obtain the default object of QSqlDatabase*/
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
/*List all the tables in the database*/
QStringList tableList = db.tables();
/*if this table does not exist we will create*/
if (!tableList.contains("linkhis"))
{
bool success = query.exec("create table linkhis (id int primary key, "
"type int,"
"infoid int,"
"geo_addr int,"
"time datetime,"
"str varchar(48))");
if (success)
{
qDebug() <<QObject::tr("linkhis successfully created!") ;
//writeFeedbackHis();
}
else
{
qDebug() <<QObject::tr("linkhis unsuccessfully created!");
return false;
}
}
return true;
}
bool Database::initOperationHis()
{
/*To obtain the default object of QSqlDatabase*/
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
/*List all the tables in the database*/
QStringList tableList = db.tables();
/*if this table does not exist we will create*/
if (!tableList.contains("operationhis"))
{
bool success = query.exec("create table operationhis (id int primary key, "
"time datetime,"
"str varchar(48))");
if (success)
{
qDebug() <<QObject::tr("operationhis successfully created!") ;
//writeOperationHis();
}
else
{
qDebug() <<QObject::tr("operationhis unsuccessfully created!");
return false;
}
}
return true;
}
void Database::writeOperationHis()
{
QDateTime dt = QDateTime::currentDateTime();
/*Before the opening of each transaction,adding this code to improve the speed*/
QSqlDatabase::database().transaction();
QSqlQuery query;
query.prepare("INSERT INTO operationhis(id, time, str)"
"VALUES (?, ?, ?)");
for (int i = 0;i<100; i++)
{
query.addBindValue(i);
query.addBindValue(dt);
query.addBindValue(QString("操作%1").arg(i));
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("insert operationhis error"));
}
}
/*Close the transaction*/
QSqlDatabase::database().commit();
}
bool Database::initShieldHis()
{
/*To obtain the default object of QSqlDatabase*/
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
/*List all the tables in the database*/
QStringList tableList = db.tables();
/*if this table does not exist we will create*/
if (!tableList.contains("shieldhis"))
{
bool success = query.exec("create table shieldhis (time datetime , "
"unitid int primary key,"
"type int)");
if (success)
{
qDebug() <<QObject::tr("shieldhis successfully created!") ;
writeShieldHis();
}
else
{
qDebug() <<QObject::tr("shieldhis unsuccessfully created!");
return false;
}
}
return true;
}
void Database::writeShieldHis()
{
QSqlDatabase::database().transaction();
QDateTime datetime = QDateTime::currentDateTime();
QSqlQuery query;
query.setForwardOnly(true);
query.prepare("INSERT INTO shieldhis( time,unitid,type)"
"VALUES ( ?, ?,?)");
query.addBindValue(datetime);
query.addBindValue(0x01000000);
query.addBindValue(0x00FFFF00);
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("in datebase : insert soundandalarmdevice into the shieldhis error"));
}
// QDateTime dt = QDateTime::currentDateTime();
// /*Before the opening of each transaction,adding this code to improve the speed*/
// QSqlDatabase::database().transaction();
// QSqlQuery query;
// query.prepare("INSERT INTO shieldhis( time,unitid,type)"
// "VALUES ( ?, ?,?)");
// int host = 1;
// int iobr = 1;
// int loop = 1;
// for (int i = 0;i<100; i++)
// {
// query.addBindValue(dt);
// query.addBindValue((host<<24) | (iobr<<16) | (loop<<8) | (i+1));
// query.addBindValue(0x000000FF);
// bool bSuccess = query.exec();
// if (!bSuccess)
// {
// QSqlError lastError = query.lastError();
// qDebug() << lastError.driverText()
// <<QString(QObject::tr("insert shieldhis error"));
// }
// }
/*Close the transaction*/
QSqlDatabase::database().commit();
}
void Database::writeFaultHis()
{
int host = 1;
int iobr = 1;
int loop = 3;
int node = 16;
int infoid = (host<<24) | (iobr<<16) | (loop<<8) | node;
int geo_addr = (host<<24) | (iobr<<16) | (loop<<8) | node;
QDateTime dt = QDateTime::currentDateTime();
/*Before the opening of each transaction,adding this code to improve the speed*/
QSqlDatabase::database().transaction();
QSqlQuery query;
query.prepare("INSERT INTO firehis(id, type, infoid, geo_addr, time, str)"
"VALUES (?, ?, ?, ?, ?, ?)");
for (int i = 0;i<100; i++)
{
query.addBindValue(i);
query.addBindValue(0x000B4000);
query.addBindValue(infoid);
query.addBindValue(geo_addr);
query.addBindValue(dt);
query.addBindValue(QString("华中科技大学南一楼%1室").arg(i));
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("insert firehis error"));
}
}
/*Close the transaction*/
QSqlDatabase::database().commit();
}
bool Database::initUnitSet()
{
/*To obtain the default object of QSqlDatabase*/
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
/*delete the table of the db*/
//query.exec("DROP TABLE unitset");
/*List all the tables in the database*/
QStringList tableList = db.tables();
/*if this table does not exist we will create*/
if (!tableList.contains("unitset"))
{
bool success = query.exec("create table unitset (id int, "
"regist bool,"
"shield bool,"
"restated bool,"
"level bool,"
"unitid int primary key,"
"geo_addr int,"
"type int,"
"delay int,"
"str varcchar(44))");
if (success)
{
qDebug() <<QObject::tr("unitset successfully created!") ;
writeUnitSet();
}
else
{
qDebug() <<QObject::tr("unitset created wrong !");
return false;
}
}
return true;
}
void Database::writeUnitSet()
{
#if 0
/*Before the opening of each transaction,adding this code to improve the speed*/
QSqlDatabase::database().transaction();
QSqlQuery query;
query.prepare("INSERT INTO unitset(id, regist, shield, restated, level, unitid, geo_addr, type, delay, str)"
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
for (int iobr = 1;iobr <= 32;iobr++)
{
for (int loop = 1;loop <= 4;loop++)
{
for (int node = 1;node <= 255;node++)
{
//if((iobr-1)*4*255+(loop-1)*255+node != rowCount++)
//qDebug() << QObject::tr("false");
query.addBindValue((iobr-1)*4*255+(loop-1)*255+node);
query.addBindValue(false);
query.addBindValue(false);
query.addBindValue(false);
query.addBindValue(false);
query.addBindValue((host<<24) | (iobr<<16) | (loop<<8) | node);
query.addBindValue(0x00000000);
query.addBindValue(0x00FFFF00);
query.addBindValue(0x00000000);
query.addBindValue(QString("华中科技大学南一楼%1室").arg(node));
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("insert unitset error"));
}
}
}
}
#endif
int host = 1;
//QDateTime dt = QDateTime::currentDateTime();
//add by qingwu :2013-4-2
/*增加节点号依次为: 01-00-00-000 的设备,
作为本机非接口板设备的报警所用,比如 声光警报器,系统主电,系统备电 等。。*/
QSqlDatabase::database().transaction();
QSqlQuery query;
query.clear();
query.prepare("INSERT INTO unitset(id, regist, shield, restated, level, unitid, geo_addr, type, delay, str)"
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
query.addBindValue(0);
query.addBindValue(true);//registed
query.addBindValue(false);//unshield
query.addBindValue(false);
query.addBindValue(false);
query.addBindValue((host<<24) | 0);//1-0-0-0
query.addBindValue(0x00000000);
query.addBindValue(0x00FFFF00);
query.addBindValue(0x00000000);
query.addBindValue(QString("本机设备"));
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("insert unitset 1-0-0-0 node error"));
}
//add by linwotian :为了33线路通信故障!为33接口板增加虚拟的回路号,并初始为1
query.clear();
query.prepare("INSERT INTO unitset(id, regist, shield, restated, level, unitid, geo_addr, type, delay, str)"
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
for (int iobr = 33,loop = 0,node = 0;iobr < 35; iobr++)//for board 33 and board 34,33-0-0-0,34-0-0-0
{
//if((iobr-1)*4*255+(loop-1)*255+node != rowCount++)
//qDebug() << QObject::tr("false");
query.addBindValue((iobr-1)*4*255);
query.addBindValue(true);//initialized unregisted
query.addBindValue(false);//initialized unshield
query.addBindValue(false);
query.addBindValue(false);
query.addBindValue((host<<24) | (iobr<<16) | (loop<<8) | node);
query.addBindValue(0x00000000);
if(iobr == 33)
query.addBindValue(0x00FF0000|(0x41<<8));//LOUXIAN 0x41
else
query.addBindValue(0x00FFFF00);
query.addBindValue(0x00000000);
if(iobr == 33)
query.addBindValue(QString("1号主机33号接口板"));
else
query.addBindValue(QString("1号主机34号接口板"));
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("insert louxian error"));
}
}
//add by linwotian ,init the zhikong information
query.clear();
query.prepare("INSERT INTO unitset(id, regist, shield, restated, level, unitid, geo_addr, type, delay, str)"
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
for (int iobr = 34, loop = 1;loop <= 20;loop++)
{
for (int node = 0;node <= 8;node++)
{
//if((iobr-1)*4*255+(loop-1)*255+node != rowCount++)
//qDebug() << QObject::tr("false");
query.addBindValue((iobr-1)*4*255+(loop-1)*8+node);
query.addBindValue(false);//initialized unregisted
query.addBindValue(false);//initialized unshield
query.addBindValue(false);
query.addBindValue(false);
query.addBindValue((host<<24) | (iobr<<16) | (loop<<8) | node);
query.addBindValue(0x00000000);
query.addBindValue(0x00FFFF00);
query.addBindValue(0x00000000);
query.addBindValue(QString("%1号直控盘%2号直控点").arg(loop).arg(node));
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("insert zhikong error"));
}
}
}
//add by qingwu :为了34线路通信故障!为34接口板增加接口板号 回路号不变 节点号为0的节点
query.clear();
query.prepare("INSERT INTO unitset(id, regist, shield, restated, level, unitid, geo_addr, type, delay, str)"
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
for (int iobr = 34,loop = 21;loop <= 84;loop++)
{
query.addBindValue((iobr-1)*4*255+(loop-1)*64 + 0);
query.addBindValue(false);//initialized unregisted
query.addBindValue(false);//initialized shield
query.addBindValue(false);
query.addBindValue(false);
query.addBindValue((host<<24) | (iobr<<16) | (loop<<8) | 0);
query.addBindValue(0x00000000);
query.addBindValue(0x00FFFF00);
query.addBindValue(0x00000000);
query.addBindValue(QString("%1号总线盘%2号总线点").arg(iobr).arg(loop));
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("insert 0 node error"));
}
}
/*Close the transaction*/
QSqlDatabase::database().commit();
}
bool Database::initBusSet()
{
/*To obtain the default object of QSqlDatabase*/
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
/*delete the table of the db*/
//query.exec("DROP TABLE busset");
/*List all the tables in the database*/
QStringList tableList = db.tables();
/*if this table does not exist we will create*/
if (!tableList.contains("busset"))
{
bool success = query.exec("create table busset (id int primary key, "
"bus_num int,"
"bus_node int,"
"inMode1 int, inMode2 int, inMode3 int, inMode4 int, inMode5 int,"
"inMode6 int, inMode7 int, inMode8 int, inMode9 int, inMode10 int,"
"inMode11 int, inMode12 int, inMode13 int, inMode14 int, inMode15 int,"
"outMode1 int, outMode2 int, outMode3 int, outMode4 int, outMode5 int,"
"outMode6 int, outMode7 int, outMode8 int, outMode9 int, outMode10 int,"
"outMode11 int, outMode12 int, outMode13 int, outMode14 int, outMode15 int)");
if (success)
{
qDebug() <<QObject::tr("busset successfully created!") ;
writeBusSet();
}
else
{
qDebug() <<QObject::tr("busset unsuccessfully created!");
return false;
}
}
return true;
}
void Database::writeBusSet()
{
/*Before the opening of each transaction,adding this code to improve the speed*/
QSqlDatabase::database().transaction();
QSqlQuery query;
query.prepare("INSERT INTO busset(id,"
"bus_num, bus_node,"
"inMode1, inMode2, inMode3, inMode4, inMode5,"
"inMode6, inMode7, inMode8, inMode9, inMode10,"
"inMode11, inMode12, inMode13, inMode14, inMode15,"
"outMode1, outMode2, outMode3, outMode4, outMode5,"
"outMode6, outMode7, outMode8, outMode9, outMode10,"
"outMode11, outMode12, outMode13, outMode14, outMode15)"
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
for (int bus_num = 1;bus_num <= 64;bus_num++)
{
for (int bus_node = 1;bus_node <= 64;bus_node++)
{
// if((bus_num-1)*128 + bus_node != rowCount++)
// qDebug() << QObject::tr("false");
query.bindValue(0, (bus_num-1)*64 + bus_node);
query.bindValue(1, bus_num);
query.bindValue(2, bus_node);
for (int i = 0; i < 15; i++)
{
query.bindValue(i+3, 0);
}
for (int j = 0; j < 15; j++)
{
query.bindValue(j+18, 0);
}
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("insert busset error"));
}
}
}
}
bool Database::initLinkSet()
{
/*To obtain the default object of QSqlDatabase*/
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
//query.exec("DROP TABLE linkset");
/*List all the tables in the database*/
QStringList tableList = db.tables();
/*if this table does not exist we will create*/
if (!tableList.contains("linkset"))
{
bool success = query.exec("create table linkset (linkid int primary key, "
"linkshield int,"
"linksetup int,"
"linktype int,"
"linknum int,"
"linkINInfo1 LongLong,"
"linkINInfo2 LongLong,"
"linkINInfo3 LongLong,"
"linkINInfo4 LongLong,"
"linkINInfo5 LongLong,"
"linkINInfo6 LongLong,"
"linkINInfo7 LongLong,"
"linkINInfo8 LongLong,"
"linkINInfo9 LongLong,"
"linkINInfo10 LongLong,"
"linkINInfo11 LongLong,"
"linkINInfo12 LongLong,"
"linkINInfo13 LongLong,"
"linkINInfo14 LongLong,"
"linkINInfo15 LongLong,"
"linkINInfo16 LongLong,"
"linkINInfo17 LongLong,"
"linkINInfo18 LongLong,"
"linkINInfo19 LongLong,"
"linkINInfo20 LongLong,"
"linkINInfo21 LongLong,"
"linkINInfo22 LongLong,"
"linkINInfo23 LongLong,"
"linkINInfo24 LongLong,"
"linkINInfo25 LongLong,"
"linkINInfo26 LongLong,"
"linkINInfo27 LongLong,"
"linkINInfo28 LongLong,"
"linkINInfo29 LongLong,"
"linkINInfo30 LongLong,"
"linkINInfo31 LongLong,"
"linkOUTInfo1 LongLong,"
"linkOUTInfo2 LongLong,"
"linkOUTInfo3 LongLong,"
"linkOUTInfo4 LongLong,"
"linkOUTInfo5 LongLong,"
"linkOUTInfo6 LongLong,"
"linkOUTInfo7 LongLong,"
"linkOUTInfo8 LongLong,"
"linkOUTInfo9 LongLong,"
"linkOUTInfo10 LongLong,"
"linkOUTInfo11 LongLong,"
"linkOUTInfo12 LongLong,"
"linkOUTInfo13 LongLong,"
"linkOUTInfo14 LongLong,"
"linkOUTInfo15 LongLong,"
"linkOUTInfo16 LongLong,"
"linkOUTInfo17 LongLong,"
"linkOUTInfo18 LongLong,"
"linkOUTInfo19 LongLong,"
"linkOUTInfo20 LongLong,"
"linkOUTInfo21 LongLong,"
"linkOUTInfo22 LongLong,"
"linkOUTInfo23 LongLong,"
"linkOUTInfo24 LongLong,"
"linkOUTInfo25 LongLong,"
"linkOUTInfo26 LongLong,"
"linkOUTInfo27 LongLong,"
"linkOUTInfo28 LongLong,"
"linkOUTInfo29 LongLong,"
"linkOUTInfo30 LongLong,"
"linkOUTInfo31 LongLong)");
if (success)
{
qDebug() <<QObject::tr("linkset successfully created!") ;
writeLinkSet();
}
else
{
qDebug() <<QObject::tr("linkset unsuccessfully created!");
return false;
}
}
return true;
}
void Database::writeLinkSet()
{
/*Before the opening of each transaction,adding this code to improve the speed*/
QSqlDatabase::database().transaction();
QSqlQuery query;
query.prepare("INSERT INTO linkset(linkid, linkshield, linksetup, linktype, linknum, "
"linkINInfo1, linkINInfo2, linkINInfo3, linkINInfo4, linkINInfo5, linkINInfo6, linkINInfo7, linkINInfo8, linkINInfo9, linkINInfo10,"
"linkINInfo11, linkINInfo12, linkINInfo13, linkINInfo14, linkINInfo15, linkINInfo16, linkINInfo17, linkINInfo18, linkINInfo19,"
"linkINInfo20, linkINInfo21, linkINInfo22, linkINInfo23, linkINInfo24, linkINInfo25, linkINInfo26, linkINInfo27, linkINInfo28, "
"linkINInfo29, linkINInfo30, linkINInfo31, linkOUTInfo1, linkOUTInfo2, linkOUTInfo3, linkOUTInfo4, linkOUTInfo5, linkOUTInfo6,"
"linkOUTInfo7, linkOUTInfo8, linkOUTInfo9, linkOUTInfo10, linkOUTInfo11, linkOUTInfo12, linkOUTInfo13, linkOUTInfo14,"
"linkOUTInfo15, linkOUTInfo16, linkOUTInfo17, linkOUTInfo18, linkOUTInfo19, linkOUTInfo20, linkOUTInfo21, linkOUTInfo22, "
"linkOUTInfo23, linkOUTInfo24, linkOUTInfo25, linkOUTInfo26, linkOUTInfo27, linkOUTInfo28, linkOUTInfo29, linkOUTInfo30, linkOUTInfo31)"
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
for (int i = 0;i<30; i++)
{
long long int host = 0;
long long int iobr = 0;
int loop = 0;
int dnode = 0;
int dline = 0;
long long int linkINInfo1 = (host<<32) | (iobr<<24) | (loop<<16) | (dnode<<8) | (dline);
int znodestar = 0;
int znodeend = 0;
int zequiptype = 0;
int zsetupnum = 0;
long long int linkOUTInfo1 = ((long long int)host<<48)
| ((long long int)iobr<<40)
| ((long long int)loop<<32)
| (znodestar<<24)
| (znodeend<<16)
| (zequiptype<<8)
| zsetupnum;
query.bindValue(0,i+1);
query.bindValue(1,0);
query.bindValue(2,0);
query.bindValue(3,4);
query.bindValue(4,1);
for (int j=0; j<31; j++)
{
query.bindValue(j+5,linkINInfo1);
}
for (int j=0; j<31; j++)
{
query.bindValue(j+36,linkOUTInfo1);
}
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("insert 联动error"));
}
}
/*Close the transaction*/
QSqlDatabase::database().commit();
}
bool Database::initIobrInfo()
{
/*To obtain the default object of QSqlDatabase*/
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
/*List all the tables in the database*/
QStringList tableList = db.tables();
/*if this table does not exist we will create*/
if (!tableList.contains("iobrinfo"))
{
bool success = query.exec("create table iobrinfo (id int primary key, "
"iobrregist bool)");
if (success)
{
qDebug() <<QObject::tr("iobrinfo successfully created!") ;
writeIobrInfo();
}
else
{
qDebug() <<QObject::tr("iobrinfo unsuccessfully created!");
}
/*add by linwot: create zhikong and zongxian table in database .*/
if (!tableList.contains("zhikonginfo"))
{
bool success1 = query.exec("create table zhikonginfo (id int primary key, "
"zhikongregist bool)");
if (success1)
{
qDebug() <<QObject::tr("zhikonginfo successfully created!") ;
writeZhikongInfo();
}
else
{
qDebug() <<QObject::tr("zhikonginfo unsuccessfully created!");
}
}
if (!tableList.contains("zongxianinfo"))
{
bool success1 = query.exec("create table zongxianinfo (id int primary key, "
"zongxianregist bool)");
if (success1)
{
qDebug() <<QObject::tr("zongxianinfo successfully created!") ;
writeZongxianInfo();
}
else
{
qDebug() <<QObject::tr("zongxianinfo unsuccessfully created!");
}
}
}
return true;
}
void Database::writeIobrInfo()
{
QSqlDatabase::database().transaction();
QSqlQuery query;
query.prepare("INSERT INTO iobrinfo(id, iobrregist)"
"VALUES (?, ?)");
for (int iobr = 1;iobr <= 34;iobr++)
{
query.addBindValue(iobr);
query.addBindValue(false);
// if ( iobr == 1 )
// {
// query.addBindValue(true);
// }
// else
// {
// query.addBindValue(false);
// }
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("iobrinfo insert error"));
}
}
QSqlDatabase::database().commit();
}
void Database::writeZongxianInfo()
{
QSqlDatabase::database().transaction();
QSqlQuery query;
query.prepare("INSERT INTO zongxianinfo(id, zongxianregist)"
"VALUES (?, ?)");
for (int zongxian = 1;zongxian <= 64;zongxian++)
{
query.addBindValue(zongxian);
query.addBindValue(false);//none regist when initialize..
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("zongxianinfo insert error"));
}
}
QSqlDatabase::database().commit();
}
void Database::writeZhikongInfo()
{
QSqlDatabase::database().transaction();
QSqlQuery query;
query.prepare("INSERT INTO zhikonginfo(id, zhikongregist)"
"VALUES (?, ?)");
for (int zhikong = 1;zhikong <= 20;zhikong++)
{
query.addBindValue(zhikong);
query.addBindValue(false);
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("zhikonginfo insert error"));
}
}
QSqlDatabase::database().commit();
}
bool Database::initPasswordInfo()
{
/*To obtain the default object of QSqlDatabase*/
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
/*List all the tables in the database*/
QStringList tableList = db.tables();
/*if this table does not exist we will create*/
if (!tableList.contains("password"))
{
bool success = query.exec("create table password (id int primary key, "
"passwordinfo varchar(20))");
if (success)
{
qDebug() <<QObject::tr("password successfully created!") ;
writePasswordInfo();
}
else
{
qDebug() <<QObject::tr("password unsuccessfully created!");
}
}
return true;
}
void Database::writePasswordInfo()
{
QSqlDatabase::database().transaction();
QSqlQuery query;
query.prepare("INSERT INTO password(id, passwordinfo)"
"VALUES (?, ?)");
for (int i = 1;i <= 2;i++)
{
query.addBindValue(i);
query.addBindValue("000000");
bool bSuccess = query.exec();
if (!bSuccess)
{
QSqlError lastError = query.lastError();
qDebug() << lastError.driverText()
<<QString(QObject::tr("password insert error"));
}