-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdeletesqlentry.cpp
47 lines (40 loc) · 1.73 KB
/
deletesqlentry.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
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>
void MainWindow::on_pushButtonDeleteEntry_clicked()
{
qDebug()<<"void MainWindow::on_pushButtonDeleteEntry_clicked()";
// on va supprimer la ligne voulue
QString txtDeleteRequest = "DELETE FROM "+ui->listWidgetTablesList->currentItem()->text()+" WHERE ";
QStringList listeDesNomsDeChamp,listeDesValeursDeChamp, listeDesConditionsDuWhere;
// récupération de la ligne actuelle
int ligneASuppr=ui->tableWidgetDeleteEntry->currentRow();
for (int counter = 0; counter < ui->tableWidgetDeleteEntry->horizontalHeader()->count(); counter++)
{
QString champ=ui->tableWidgetDeleteEntry->horizontalHeaderItem(counter)->text();
QString valeur=ui->tableWidgetDeleteEntry->item(ligneASuppr,counter)->text().replace("'","''");
// on ajoute à notre condition
if(valeur.isEmpty()) {
valeur += "NULL";
listeDesConditionsDuWhere.append(champ+"="+valeur);
} else {
listeDesConditionsDuWhere.append(champ+"='"+valeur+"'");
}
}
QString WHERE=listeDesConditionsDuWhere.join(" AND ");
txtDeleteRequest += WHERE;
QSqlQuery deleteRequest(txtDeleteRequest);
qDebug()<<txtDeleteRequest;
// on exécute la requête
if(deleteRequest.exec()) {
// la requête est fonctionnelle
ui->statusBar->showMessage("The SQL request has been executed sucessfully.",8000);
ui->statusBar->setStyleSheet("color: #1d912d;");
} else {
// la requête n'est pas fonctionnelle
ui->statusBar->showMessage(deleteRequest.lastError().text(),8000);
ui->statusBar->setStyleSheet("color: #a62929;");
}
}