Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt my sql insert record values to be added later how to do it
Forum Updated to NodeBB v4.3 + New Features

Qt my sql insert record values to be added later how to do it

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 3.3k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    hemendar
    wrote on last edited by
    #1

    in this code please view values secion

    #include "stockentry.h"
    #include "ui_stockentry.h"
    #include "stock_manager.h"

    StockEntry::StockEntry(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::StockEntry)
    {
    ui->setupUi(this);

    }

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

    void StockEntry::on_pushButton_clicked()
    {

    QSqlQuery qry;
    qry.prepare( "INSERT INTO hemendar (Date, BaleNumber) VALUES ()" );
    
           
      if( !qry.exec() )
        qDebug() << qry.lastError();
      else
        qDebug( "Inserted!" );
    

    }

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

      Hi and welcome to devnet,

      Please enclose your code with coding tags, it will make it readable.

      What it is you want exactly ?

      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
      • H Offline
        H Offline
        hemendar
        wrote on last edited by
        #3

        sir i want the code for what ever i type in line edit should be added to
        mysql database (odbc) when i click the push button

        thanks in advance sir

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rakor
          wrote on last edited by
          #4

          I'm not really shure wht you need. But you have to open a database-connection before you can write (ore read) into the database.

          Have you had a look at "this":http://qt-project.org/doc/qt-5.0/qtsql/sql-programming.html ?

          1 Reply Last reply
          0
          • H Offline
            H Offline
            hemendar
            wrote on last edited by
            #5

            sir first of all thank you very much for your quick replay, i am very new to qt, sir, i am using mysql odbc drivers (the code of which i will give you below) in my project i am haveing some options like date and bale number e.t.c , so i want to write a quary that when ever a person types in line edit box infront of date label or bale number label i want that data to be inserted into the database table in date and bale number respectively on click of push button, here is my code sir

            @stock.pro

            QT += core gui sql

            headers

            stock_manager.h

            #ifndef STOCK_MANAGER_H
            #define STOCK_MANAGER_H

            #include "stockentry.h"
            #include <QMainWindow>
            #include "QDebug"
            #include "QtSql"
            #include "QtCore"
            #include "QtGui"

            namespace Ui {
            class Stock_Manager;
            }

            class Stock_Manager : public QMainWindow
            {
            Q_OBJECT

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

            private slots:
            void on_pushButton_clicked();

            private:
            Ui::Stock_Manager *ui;

            StockEntry *stockentry;
             QSqlDatabase db;
            QSqlQuery qry;
            

            };

            #endif // STOCK_MANAGER_H

            stock_manager.cpp

            #include "stock_manager.h"
            #include "ui_stock_manager.h"
            #include "stockentry.h"

            Stock_Manager::Stock_Manager(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::Stock_Manager)
            {
            ui->setupUi(this);

            db = QSqlDatabase::addDatabase("QODBC");
                db.setHostName("127.0.0.1");
                db.setDatabaseName("hemendar");
            
                if(!db.open())
                   ui->label->setText("failed to connect");
                else
                    ui->label->setText("connected.......");
            

            }

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

            void Stock_Manager::on_pushButton_clicked()
            }

            void Stock_Manager::on_pushButton_2_clicked()
            {
            stockentry =new StockEntry(this);
            stockentry->show();
            }

            stockentry.cpp

            #include "stockentry.h"
            #include "ui_stockentry.h"
            #include "stock_manager.h"
            #include <qsql.h>
            #include <QLineEdit>

            StockEntry::StockEntry(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::StockEntry)
            {
            ui->setupUi(this);

            }

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

            void StockEntry::on_pushButton_clicked()
            {

            QSqlQuery query;
            query.prepare("INSERT INTO hemendar (Date, BaleNumber) "
            "VALUES (?, ?)");
            query.addBindValue();
            query.addBindValue();
            
            query.exec(&#41;;@
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              The have a look "here":http://qt-project.org/doc/qt-5.0/qtsql/sql-forms.html and search for the book manager example. It shows why you can use to achieve what you want.

              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
              • H Offline
                H Offline
                hemendar
                wrote on last edited by
                #7

                Sir i created a sample program in which, When a user types date in line edit and click on save button it saves the date into my sql data base. The program got build success fully but when i click save button this program crashes with error. I Have posted code please help me!

                @
                hemiya.pro

                QT += core gui sql

                main window.h

                #ifndef MAINWINDOW_H
                #define MAINWINDOW_H

                #include <QMainWindow>
                #include <QtSql>
                #include <QtDebug>
                #include <qfileinfo>
                #include <QtCore>
                #include <QLineEdit>

                namespace Ui {
                class MainWindow;
                }

                class MainWindow : public QMainWindow
                {
                Q_OBJECT

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

                private slots:
                void on_Save_clicked();

                private:
                Ui::MainWindow *ui;
                QLineEdit *lineDate;
                };

                #endif // MAINWINDOW_H

                main window.cpp

                #include "mainwindow.h"
                #include "ui_mainwindow.h"
                #include <qsql.h>
                #include <QLineEdit>

                MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
                {
                ui->setupUi(this);

                QSqlDatabase mydb=QSqlDatabase::addDatabase("QSQLITE");
                mydb.setDatabaseName("J:/Stock");
                if(!mydb.open())
                    ui->label->setText("failed to open the database");
                else
                    ui->label->setText("connected.....");
                

                }

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

                void MainWindow::on_Save_clicked()

                {
                QSqlQuery query("insert into hemendar (Date) values (?)");

                query.bindValue(0,lineDate->text());
                query.exec();
                @

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

                  lineDate is not initialized.

                  Since you're using a ui file, you have that line edit there

                  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
                  • H Offline
                    H Offline
                    hemendar
                    wrote on last edited by
                    #9

                    how to initialize it sir, i am very new and i read everything but unable to understand.
                    Kindly if you can tell me how to do that or give me a hint, please help me sir! Thanks in advance

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      deepakberiwal
                      wrote on last edited by
                      #10

                      You can simply initialize lineDate in the constructor as:

                      @lineDate = new QLineEdit();@

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

                        Don't you have that QLineEdit in your designer made UI ? If so then use that and remove lineDate from your header.

                        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