-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtablelist.cpp
115 lines (97 loc) · 3.92 KB
/
tablelist.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
#include <QDebug>
#include <QString>
#include <QSqlQuery>
#include <QDialog>
#include <QSqlRecord>
#include "ui_mainwindow.h"
#include "mainwindow.h"
void MainWindow::afficherTable()
{
QString txtReq;
txtReq=("SHOW TABLES");
QSqlQuery req(txtReq);
qDebug()<<txtReq;
while(req.next())
{
ui->listWidgetTablesList->addItem(req.value(0).toString());
}
}
void MainWindow::on_listWidgetTablesList_itemClicked(QListWidgetItem *item)
{
// on active le bouton pour supprimer le bouton
ui->pushButtonDeleteTable->setEnabled(1);
//Initialisation du tableau
ui->tableWidgetStructure->setRowCount(0);
ui->tableWidgetStructure->setColumnCount(0);
ui->tableWidgetStructure->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->tableWidgetRequestInsert->setRowCount(0);
ui->tableWidgetRequestInsert->setColumnCount(0);
ui->tableWidgetRequestInsert->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->tableWidgetRequestInsert->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->tableWidgetDeleteEntry->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// requête SQL
QString txtReq= "SELECT * FROM "+ui->listWidgetTablesList->currentItem()->text();
QSqlQuery req(txtReq);
qDebug()<<txtReq;
int compteur= 0;
QSqlRecord recordRequest = req.record();
int nbChamp=recordRequest.count();
ui->tableWidgetStructure->setColumnCount(nbChamp);
ui->tableWidgetDeleteEntry->setColumnCount(nbChamp);
ui->tableWidgetRequestInsert->setRowCount(nbChamp);
//affichage des noms de colonne
for(int noCol=0;noCol<nbChamp;noCol++)
{
ui->tableWidgetStructure->setHorizontalHeaderItem(noCol,new QTableWidgetItem(recordRequest.fieldName(noCol)));
ui->tableWidgetDeleteEntry->setHorizontalHeaderItem(noCol,new QTableWidgetItem(recordRequest.fieldName(noCol)));
}
//affichage des noms de ligne (inserer)
for(int noRow=0;noRow<nbChamp;noRow++)
{
ui->tableWidgetRequestInsert->setHorizontalHeaderItem(noRow,new QTableWidgetItem(recordRequest.fieldName(noRow)));
}
//affichage des valeurs
while(req.next())
{
// on insère la ligne
ui->tableWidgetStructure->setRowCount(compteur+1);
ui->tableWidgetDeleteEntry->setRowCount(compteur+1);
ui->tableWidgetRequestInsert->setColumnCount(1);
for(int counter = 0; counter < recordRequest.count(); counter++) {
// on affiche l'item
ui->tableWidgetStructure->setItem(compteur,counter,new QTableWidgetItem(req.value(counter).toString()));
ui->tableWidgetDeleteEntry->setItem(compteur,counter,new QTableWidgetItem(req.value(counter).toString()));
ui->tableWidgetRequestInsert->setVerticalHeaderItem(counter,new QTableWidgetItem(recordRequest.fieldName(counter)));
QLineEdit* valueLineEdit = new QLineEdit();
ui->tableWidgetRequestInsert->setCellWidget(counter,0,valueLineEdit);
}
// on ajoute une ligne
compteur+=1;
}
}
void MainWindow::on_pushButtonInsertExecut_clicked()
{
QString txtReqInsert= "SELECT * FROM "+ui->listWidgetTablesList->currentItem()->text();
QSqlQuery req(txtReqInsert);
qDebug()<<txtReqInsert;
QSqlRecord recordRequest = req.record();
int nbChamp=recordRequest.count();
qDebug()<<nbChamp;
QString resultat;
for(int i = 0; i < nbChamp; i++)
{
QString test=((QLineEdit *)(ui->tableWidgetRequestInsert->cellWidget(i,0)))->text();
if(test.isEmpty())
{
test="NULL";
}
resultat+=" '"+test+"'";
}
QStringList listeElement=resultat.split(" ");
qDebug()<<listeElement;
QString txtRequet="INSERT INTO "+ui->listWidgetTablesList->currentItem()->text()+" VALUES (";
txtRequet+=listeElement.join(",");
txtRequet+=");";
qDebug()<<txtRequet;
QSqlQuery query(txtRequet);
}