Hello, I would like to ask for a help.
I would like to insert into datas, in to my database with pushbutton.
I use QT Creator 3.3.0 and SQLITE. I got this error "no query to unable to fetch row". Please help me.
My header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtSql>
#include <QDebug>
#include <QFileInfo>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
void connClose()
{
mydb.close();
}
bool connOpen()
{
mydb.setDatabaseName("D:/Szilvi/Szakdolgozat/Modellezo.db");
if(!mydb.open()){
qDebug()<<("Nem sikerult megnyitni az adatbazist");
return false;}
else{
qDebug()<<("megnyitva");
return true;}
}
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private slots:
void on_pushButton_3_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtSql>
#include <QDebug>
#include <QFileInfo>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
QSqlDatabase mydb;
void connClose()
{
mydb.close();
mydb.removeDatabase(QSqlDatabase::defaultConnection);
}
bool connOpen()
{
QSqlDatabase mydb=QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("D:/Szilvi/Szakdolgozat/Modellezo.db");
if(!mydb.open()){
qDebug()<<("Nem sikerult megnyitni az adatbazist");
return false;}
else{
qDebug()<<("megnyitva");
return true;}
}
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_3_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
My mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
if(!connOpen())
ui->label_3->setText("Cannot connect to the database");
else
ui->label_3->setText("Connect");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow:: on_pushButton_3_clicked()
{
connOpen();
EgyedNev=ui->lineEdit_egyednev->text();
EgyedKulcs=ui->lineEdit_egyedkulcs->text();
connOpen();
qry.prepare("INSERT INTO Egyed (ENev,EKulcs) VALUE ('"+EgyedNev+"','"+EgyedKulcs+"')");
if(qry.exec())
{
connClose();
}
else
{
QMessageBox::critical(this,tr
("error::"),qry.
lastError().
text());
}
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
if(!connOpen())
ui->label_3->setText("Cannot connect to the database");
else
ui->label_3->setText("Connect");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow:: on_pushButton_3_clicked()
{
connOpen();
QString EgyedNev,EgyedKulcs;
EgyedNev=ui->lineEdit_egyednev->text();
EgyedKulcs=ui->lineEdit_egyedkulcs->text();
connOpen();
QSqlQuery qry;
qry.prepare("INSERT INTO Egyed (ENev,EKulcs) VALUE ('"+EgyedNev+"','"+EgyedKulcs+"')");
if(qry.exec())
{
QMessageBox::critical(this,tr("Save"),tr("Saved"));
connClose();
}
else
{
QMessageBox::critical(this,tr("error::"),qry.lastError().text());
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks