Results 1 to 3 of 3

Thread: "no query to unable to fetch row"

  1. #1
    Join Date
    Mar 2015
    Posts
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default "no query to unable to fetch row"

    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:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtSql>
    6. #include <QDebug>
    7. #include <QFileInfo>
    8.  
    9.  
    10. namespace Ui {
    11. class MainWindow;
    12. }
    13.  
    14. class MainWindow : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19.  
    20. void connClose()
    21. {
    22. mydb.close();
    23. mydb.removeDatabase(QSqlDatabase::defaultConnection);
    24. }
    25.  
    26. bool connOpen()
    27. {
    28. QSqlDatabase mydb=QSqlDatabase::addDatabase("QSQLITE");
    29. mydb.setDatabaseName("D:/Szilvi/Szakdolgozat/Modellezo.db");
    30.  
    31. if(!mydb.open()){
    32. qDebug()<<("Nem sikerult megnyitni az adatbazist");
    33. return false;}
    34. else{
    35. qDebug()<<("megnyitva");
    36. return true;}
    37. }
    38.  
    39. public:
    40. explicit MainWindow(QWidget *parent = 0);
    41. ~MainWindow();
    42.  
    43. private slots:
    44.  
    45. void on_pushButton_3_clicked();
    46.  
    47. private:
    48. Ui::MainWindow *ui;
    49.  
    50. };
    51.  
    52. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    My mainwindow.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QMessageBox>
    4.  
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11.  
    12. if(!connOpen())
    13. ui->label_3->setText("Cannot connect to the database");
    14. else
    15. ui->label_3->setText("Connect");
    16. }
    17.  
    18. MainWindow::~MainWindow()
    19. {
    20. delete ui;
    21. }
    22.  
    23. void MainWindow:: on_pushButton_3_clicked()
    24. {
    25. connOpen();
    26. QString EgyedNev,EgyedKulcs;
    27. EgyedNev=ui->lineEdit_egyednev->text();
    28. EgyedKulcs=ui->lineEdit_egyedkulcs->text();
    29.  
    30. connOpen();
    31. QSqlQuery qry;
    32. qry.prepare("INSERT INTO Egyed (ENev,EKulcs) VALUE ('"+EgyedNev+"','"+EgyedKulcs+"')");
    33. if(qry.exec())
    34. {
    35. QMessageBox::critical(this,tr("Save"),tr("Saved"));
    36. connClose();
    37. }
    38. else
    39. {
    40. QMessageBox::critical(this,tr("error::"),qry.lastError().text());
    41. }
    42.  
    43. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 26th March 2015 at 10:07. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: "no query to unable to fetch row"

    Qt Code:
    1. bool connOpen()
    2. {
    3. QSqlDatabase mydb=QSqlDatabase::addDatabase("QSQLITE");
    4. mydb.setDatabaseName("D:/Szilvi/Szakdolgozat/Modellezo.db");
    5.  
    6. if(!mydb.open()){
    7. qDebug()<<("Nem sikerult megnyitni az adatbazist");
    8. return false;}
    9. else{
    10. qDebug()<<("megnyitva");
    11. return true;}
    12. }
    To copy to clipboard, switch view to plain text mode 

    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.

  3. #3
    Join Date
    Mar 2015
    Posts
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: "no query to unable to fetch row"

    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.
    Last edited by Sziszke; 28th March 2015 at 12:50.

Similar Threads

  1. Replies: 3
    Last Post: 16th March 2015, 07:31
  2. SQLITTE "unable to fetch row"
    By giusepped in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2010, 23:24
  3. Replies: 9
    Last Post: 20th May 2010, 09:55
  4. Unable to fetch data when searching through select query
    By sinha.ashish in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2008, 14:06
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.