PDA

View Full Version : "no query to unable to fetch row"



Sziszke
26th March 2015, 10:00
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;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
QSqlDatabase mydb;

void connClose()
{
mydb.close();
mydb.removeDatabase(QSqlDatabase::defaultConnectio n);
}

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


My mainwindow.cpp:


#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());
}

}

d_stranz
26th March 2015, 16:58
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;}
}


This code declares a local variable named "mydb", which is not the same as the variable declared in the class definition. So this code creates the DB instance, opens it, and then it immediately goes out of scope (and is destroyed, along with the DB connection) when the method exits. The variable declared in the class is never initialized or connected to anything.

Sziszke
28th March 2015, 12:50
Thanks for your help.
My next problem is, I would like to insert datas to my database from my form.
I think this part is incorrect:

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+"')");

Please help.

Added after 16 minutes:

Problem solved. :)