Qt my sql insert record values to be added later how to do it
-
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!" );
}
-
Hi and welcome to devnet,
Please enclose your code with coding tags, it will make it readable.
What it is you want exactly ?
-
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 buttonthanks in advance sir
-
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 ?
-
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_OBJECTpublic:
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();@
-
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.
-
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!
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_OBJECTpublic:
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();
@ -
lineDate is not initialized.
Since you're using a ui file, you have that line edit there
-
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 -
You can simply initialize lineDate in the constructor as:
@lineDate = new QLineEdit();@
-
Don't you have that QLineEdit in your designer made UI ? If so then use that and remove lineDate from your header.