Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Urgent!! Unable to create and insert values into database

Urgent!! Unable to create and insert values into database

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
14 Posts 2 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS SGaist

    Hi,

    You haven't opened the database connection before executing your queries.

    J Offline
    J Offline
    Jocelyn
    wrote on last edited by
    #3

    @SGaist
    thanks in advance..
    but my problem now is that l cant even create my table Account and insert data into it...
    that's why l put database connection..
    erm, l'll remove it after this...

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #4

      You are also not checking if your queries are running successfully. Add them and if they fail print the error string to have some clues about what is going wrong.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      1
      • SGaistS SGaist

        You are also not checking if your queries are running successfully. Add them and if they fail print the error string to have some clues about what is going wrong.

        J Offline
        J Offline
        Jocelyn
        wrote on last edited by
        #5

        @SGaist
        actually error comes out..l cant detect where's my problem..sry...ahha
        is my query sth wrong??

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #6

          Care to share the error message(s) ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 2 Replies Last reply
          1
          • SGaistS SGaist

            Care to share the error message(s) ?

            J Offline
            J Offline
            Jocelyn
            wrote on last edited by
            #7

            @SGaist
            thanks =]

            Starting C:\Users\user\Documents\BITI Sem 3\Bengkel 1 Qt\build-SIDEACC-Desktop_Qt_5_5_0_MSVC2013_64bit-Debug\debug\SIDEACC.exe...
            
            QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
            
            QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
            
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #8

              That's a warning, not an error. You are calling addDatabase several time so you are replacing the current connection (in this case the default connection) each time.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • SGaistS SGaist

                Care to share the error message(s) ?

                J Offline
                J Offline
                Jocelyn
                wrote on last edited by
                #9

                @SGaist
                or is it that l didnt declare my header of Account in other place else??

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  It's not a declaration problem, are you instantiating Register several times ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  J 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    It's not a declaration problem, are you instantiating Register several times ?

                    J Offline
                    J Offline
                    Jocelyn
                    wrote on last edited by
                    #11

                    @SGaist
                    erm...no, l think..
                    maybe l show you my register.h, main.cpp and logininterface.cpp

                    //register.h
                    #ifndef REGISTER_H
                    #define REGISTER_H
                    
                    #include <QDialog>
                    #include<QtSQL>
                    
                    namespace Ui {
                    class Register;
                    }
                    
                    class Register : public QDialog
                    {
                        Q_OBJECT
                    
                    public:
                        explicit Register(QWidget *parent = 0);
                        ~Register();
                    
                    private slots:
                        void on_registerPushButton_clicked();
                    
                    
                    private:
                        Ui::Register *ui;
                    
                    };
                    
                    #endif // REGISTER_H
                    
                    
                    //main.cpp
                    #include "logininterface.h"
                    #include "register.h"
                    #include <QTime>
                    #include <QDebug>
                    #include <QtSql>
                    #include <QMessageBox>
                    #include <QApplication>
                    #include <QSqlQuery>
                    #include <QSqlError>
                    #include <QMessageBox>
                    #include <QSplashScreen>
                    #include <QTimer>
                    
                    bool buatConnection();
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                    
                        QSplashScreen *splash = new QSplashScreen;
                        splash->setPixmap(QPixmap("‪C:/Users/user/Documents/"
                                                  "BITI Sem 3/Bengkel 1 Qt/Demo/shop.png"));
                        splash->show();
                    
                        LoginInterface w;
                        //w.show();
                    
                        QTimer::singleShot(5000,splash,SLOT(close()));
                        QTimer::singleShot(5000,&w,SLOT(show()));
                    
                        /*LoginInterface w;
                        w.show();
                        if(!buatConnection())
                            return false;
                        return a.exec();*/
                    
                        if(!buatConnection())
                            return false;
                    
                        return a.exec();
                    }
                    
                    bool buatConnection()
                    {
                        QSqlDatabase dbSaya = QSqlDatabase::addDatabase("QSQLITE");
                    
                        dbSaya.setDatabaseName("C:/Users/user/Documents/BITI Sem 3/Bengkel 1 Qt/shop.db");
                    
                        if(!dbSaya.open())
                        {
                            QMessageBox::critical(0, QObject::tr("Database failed to open..."),
                                                  dbSaya.lastError().text());
                            return false;
                        }
                    
                        else
                    
                        //no need to show, for not to spoilt
                        /*else
                            QMessageBox::information(0,"Database Connection","Database is connected successfully!");*/
                        return true;
                    }
                    
                    
                    //logininterface.cpp
                    #include "logininterface.h"
                    #include "ui_logininterface.h"
                    #include <QTime>
                    #include <QDebug>
                    #include <QtSql>
                    #include <QMessageBox>
                    #include <QApplication>
                    #include <QSqlQuery>
                    #include <QSqlError>
                    #include <QMessageBox>
                    #include <QSqlError>
                    
                    
                    LoginInterface::LoginInterface(QWidget *parent) :
                        QDialog(parent),
                        ui(new Ui::LoginInterface)
                    {
                        ui->setupUi(this);
                    
                        QPixmap pix
                                ("C:/Users/user/Documents/BITI Sem 3/Bengkel 1 Qt/article-image-welcome.jpg");
                        ui->picLabel->setPixmap(pix);
                        //C:/Users/user/Documents/BITI Sem 3/Bengkel 1 Qt/article-image-welcome.jpg
                    
                        QSqlDatabase dbSaya= QSqlDatabase::addDatabase("QSQLITE");
                    
                        dbSaya.setDatabaseName
                                ("C:/Users/user/Documents/BITI Sem 3/Bengkel 1 Qt/shop.db");
                    
                        if(!dbSaya.open())
                            QMessageBox::critical(0, QObject::tr("Login failed to open..."),
                                                  dbSaya.lastError().text());
                        //no need to show, for not to spoilt
                        /*else
                        {
                    
                            QMessageBox::information(0,"Database Interface",
                                                     "Welcome to this system,"
                                                     "\nplease login before proceeding...\t");
                        }*/
                    }
                    
                    
                    LoginInterface::~LoginInterface()
                    {
                        delete ui;
                    }
                    
                    void LoginInterface::on_loginPushButton_clicked()
                    {
                        QString userId,passWord;
                        userId=ui->userIdLineEdit->text();
                        passWord=ui->passwordLineEdit->text();
                    
                        ui->userIdLineEdit->setText(userId);
                        ui->passwordLineEdit->setText(passWord);
                    
                        QSqlQuery querySaya;
                       /*querySaya.prepare("CREATE TABLE IF NOT EXISTS Login
                        * (userId VARCHAR(12)PRIMARY KEY, passWord VARCHAR(20))");
                    
                        if (!querySaya.exec())
                        {
                            //QSqlError lastError();
                            //QSqlQuery::lastError();
                            //QSqlError lastError()
                    
                            qDebug()<<querySaya.lastError().text();
                        }
                        else
                            qDebug()<<"Table created";*/
                    
                        querySaya.prepare("SELECT * FROM Login where "
                                          "userId='"+userId+"' and passWord='"+passWord+"')");
                    
                        if(querySaya.next())
                        {
                            QMessageBox::warning(this,"Login successfully",
                                                 querySaya.lastError().text());
                        }
                        else
                        {
                            ui->statusLabel->setText("[!] Invalid UserID "
                                                     "and Password..\n  "
                                                     "Please enter again! ");
                            ui->userIdLineEdit->setText(userId);
                            ui->passwordLineEdit->setText(passWord);
                        }
                    
                    
                        /*if (querySaya.exec("SELECT * FROM Login WHERE
                         * userID='"+userId+"'and passWord='"+passWord+"'"))
                        {
                    
                            if(passWord == passWord)
                            {
                                if (userId == userId)
                                {
                                    QMessageBox::information(this,"USER ID","Matched");
                                    this->hide();
                                    tetingkapSideacc.show();
                    
                                }
                    
                    
                                else
                                {
                                    QMessageBox::critical(this,"ERROR",querySaya.lastError().text());
                                }
                                //QMessageBox::information(this,"PASSWORD","Password Matched, Login!");
                            }
                    
                            else
                            {
                    
                                QMessageBox::critical(this,"PASSWORD ERROR",
                                    querySaya.lastError().text());
                            }
                    
                        }*/
                    
                    
                        if(querySaya.exec("SELECT * FROM Login WHERE "
                                          "userID='"+userId+"'and passWord='"+passWord+"'"))
                        {
                            int count = 0;
                            while (querySaya.next())
                            {
                                count++;
                            }
                            if(count==1)
                            {
                                //ui->userIdLineEdit->setText(userId);
                                //ui->passwordLineEdit->setText(passWord);
                                ui->statusLabel->setText("User ID and Password are correct. "
                                                         "Login successfully!");
                                ui->userIdLineEdit->setText(userId);
                                ui->passwordLineEdit->setText(passWord);
                                this->hide();
                                tetingkapSideacc.show();
                            }
                    
                            if(count>1)
                            {
                                ui->statusLabel->setText("Duplicate User ID and Password. "
                                                         "Please reset.");
                                ui->userIdLineEdit->clear();
                                ui->passwordLineEdit->clear();
                            }
                    
                            if(count<1)
                            {
                                ui->statusLabel->setText("User ID and Password are not correct.\n"
                                                         "Please reset.");
                                ui->userIdLineEdit->clear();
                                ui->passwordLineEdit->clear();            
                            }
                        }
                    }
                    
                    void LoginInterface::on_resetPushButton_clicked()
                    {
                        QString userId,passWord;
                        ui->userIdLineEdit->clear();
                        ui->passwordLineEdit->clear();
                        ui->userIdLineEdit->setText(userId);
                        ui->passwordLineEdit->setText(passWord);
                    }
                    
                    /*void LoginInterface::on_commandLinkButton_clicked()
                    {
                        QString userId,passWord;
                        userId=ui->userIdLineEdit->text();
                        passWord=ui->passwordLineEdit->text();
                    
                        ui->userIdLineEdit->setText(userId);
                        ui->passwordLineEdit->setText(passWord);
                    
                        if(querySaya.exec("SELECT * FROM Login WHERE "
                                          "userID='"+userId+"'and passWord='"+passWord+"'"))
                        {
                            int count = 0;
                            while (querySaya.next())
                            {
                                count++;
                            }
                    
                            if(count<1)
                            {
                                ui->statusLabel->setText("User ID and Password are not correct.\n"
                                                         "Please reset.");
                                ui->userIdLineEdit->clear();
                                ui->passwordLineEdit->clear();
                            }
                    }*/
                    
                    
                    void LoginInterface::on_forgotPswdCommandLinkButton_clicked()
                    {
                        tetingkapResetPassword.show();
                    
                        /*QString userId,passWord;
                        ui->userIdLineEdit->setText(userId);
                        ui->passwordLineEdit->setText(passWord);
                    
                        QSqlQuery querySaya;
                        if(querySaya.exec("SELECT * FROM Login WHERE "
                                          "userID='"+userId+"'"))
                        {
                            int count = 0;
                            while (querySaya.next())
                            {
                                count++;
                            }
                            if(count==1)
                            {
                                QMessageBox::information
                                        (this,"Reset password",
                                         "Reset here");
                                QMessageBox ::StandardButton reply;
                                if (querySaya.exec("SELECT * FROM Login "
                                               "WHERE Password='"+passWord+"'"))
                                {
                                    reply = QMessageBox::information
                                                (this,"Change Password",
                                                 "Password is changed successfully!");
                                }
                            }
                        }*/
                    }
                    
                    void LoginInterface::on_createAccPushButton_clicked()
                    {
                        tetingkapRegister.show();
                    }
                    
                    
                    //logininterface.h
                    #ifndef LOGININTERFACE_H
                    #define LOGININTERFACE_H
                    
                    #include <QDialog>
                    #include <QtSQL>
                    #include "sideacc.h"
                    #include "register.h"
                    #include "resetpassword.h"
                    
                    namespace Ui {
                    class LoginInterface;
                    }
                    
                    class LoginInterface : public QDialog
                    {
                        Q_OBJECT
                    
                    public:
                        explicit LoginInterface(QWidget *parent = 0);
                        ~LoginInterface();
                    
                    private slots:
                        void on_loginPushButton_clicked();
                    
                        void on_resetPushButton_clicked();
                    
                        void on_forgotPswdCommandLinkButton_clicked();
                    
                        void on_createAccPushButton_clicked();
                    
                    private:
                        Ui::LoginInterface *ui;
                    protected:
                        SIDEACC tetingkapSideacc;
                        Register tetingkapRegister;
                        ResetPassword tetingkapResetPassword;
                    
                    };
                    
                    #endif // LOGININTERFACE_H
                    
                    
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      You have at least three calls to addDatabase. That's not useful. Do it once e.g. in your main function. There's no need to call addDatabase several time if you are only going to use one connection.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      J 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        You have at least three calls to addDatabase. That's not useful. Do it once e.g. in your main function. There's no need to call addDatabase several time if you are only going to use one connection.

                        J Offline
                        J Offline
                        Jocelyn
                        wrote on last edited by
                        #13

                        @SGaist
                        oh l see..l've removed them..thanks =]
                        but my problem is that my table Account is unable to be created in my databse..

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #14

                          Again, check the return value of exec and print the content of errorString if the query failed. That will give hints about what is going on.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved