Database and qt [☺SOLVED]



  • hello evreybody
    please help

    ther is my code
    @#ifndef LOGINWINDOW_H
    #define LOGINWINDOW_H

    #include <QMainWindow>
    #include <QtSql>
    #include<QtDebug>
    #include<QFileInfo>
    namespace Ui {
    class loginWindow;
    }

    class loginWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit loginWindow(QWidget *parent = 0);
    ~loginWindow();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::loginWindow *ui;
    QSqlDatabase db;
    };

    #endif // LOGINWINDOW_H@

    mainwindow.cpp

    @#include "loginwindow.h"
    #include "ui_loginwindow.h"
    using namespace std;
    #define q2c(string) string.toStdString()
    loginWindow::loginWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::loginWindow)
    {
    ui->setupUi(this);
    //conection a la base de donnees
    db= QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("yaki");
    if(!db.open())

        ui->stats->setText("fail to open the database");
    
    else
    ui->stats->setText("connected");
    
    }
    

    loginWindow::~loginWindow()
    {
    delete ui;
    }

    void loginWindow::on_pushButton_clicked()
    {
    QString username,password;
    username=ui->username->text();
    password=ui->password->text();
    if(!db.isOpen())
    {
    qDebug()<<"fail to open";
    return;

    }
    QSqlQuery query;
    if (query.exec&#40;"SELECT*FROM utilisateur WHERE identificateur='"+username+"'and
    

    password='"+password+"'"))
    {
    int count(0);
    while(query.next()){
    count++;

        }
        if (count==1)
            ui->stats->setText("bravo login");
    }
    

    }@

    so am trying to creat a login window but this part of code don't work

    @void loginWindow::on_pushButton_clicked()
    {
    QString username,password;
    username=ui->username->text();
    password=ui->password->text();
    if(!db.isOpen())
    {
    qDebug()<<"fail to open";
    return;

    }
    QSqlQuery query;
    if (query.exec&#40;"SELECT*FROM utilisateur WHERE identificateur='"+username+"'&& password='"+password+"'"&#41;&#41;
    {
        int count(0&#41;;
        while(query.next()){
            count++;
    
        }
        if (count==1)
            ui->stats->setText("bravo login");
    }
    

    }@

    there are an somthing rong in the last part of the code because wfen i run the project there is no error evrything is right but when i pressed the login button after i inter the username and the password the label don't change



  • There should be space between "SELECT", "*" and "FROM", and also around "and", in your query. "&&" is not validSQL.

    You should look at lastError() when the query exec() fails.

    BTW: You should use bind variables in the query rather than building a string the way you have. Ponder what would happen if I entered this as as the password: @whatever' OR 1=1 @



  • HI
    THANK YOU CHRISw67


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.