Results 1 to 12 of 12

Thread: [QSqlQuery] Problem with not open database

  1. #1
    Join Date
    Jul 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [QSqlQuery] Problem with not open database

    Hi folks. I'm trying to write simple database program base on MySQL. I've established connection with database but I'can query. Here is what I'm talking about:
    Qt Code:
    1. #include "myquery.h"
    2.  
    3. MyQuery::MyQuery(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. db=QSqlDatabase::addDatabase("QMYSQL", "lge");
    7. db.setDatabaseName("WR");
    8. db.setHostName("localhost");
    9. db.setUserName("root");
    10. db.setPassword("password");
    11. using std::cout;
    12. if(db.open())
    13. {
    14. cout<<"Database opend.\n";
    15. }
    16.  
    17. cout<<query.exec("SELECT * FROM items");
    18. }
    To copy to clipboard, switch view to plain text mode 
    I'm trying to run this under WinXP. I've added "CONFIG+=console" to see std output and I'm getting:
    Database opend.
    QSqlQuery::exex:database not open
    0
    What I'm doing wrong?
    Last edited by xkazielx; 20th July 2009 at 13:17. Reason: updated contents

  2. #2
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: [QSqlQuery] Problem with not open database

    is the QSQLQuery query connected to the database? I'm missing

    Qt Code:
    1. QSqlQuery query(db);
    To copy to clipboard, switch view to plain text mode 

    on the other hand, to avoid strange problems, always try to execute "prepare" before executing.

  3. #3
    Join Date
    Jul 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QSqlQuery] Problem with not open database

    Thanks, that helped me with "SELECT". But when I was doing INSERT I've saw:
    C:/Documents and Settings/user/My Documents/queryTest/MyQuery/main.cpp:28: error: no matching function for call to `QSqlQuery::addBindValue(int)'
    c:/Qt/4.5.2/include/QtSql/../../src/sql/kernel/qsqlquery.h:113: note: candidates are: void QSqlQuery::addBindValue(const QVariant&, QSql::ParamType)
    When I've included QVariant it started working as a charm.
    Thanks 4 your help.

  4. #4
    Join Date
    Jul 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QSqlQuery] Problem with not open database

    Hi friends!!! I'm still fighting with SQL app. Now I've got a derived class with a slot that is connected to a signal emited by MainWindow class (top class in my pro). In a class I have two variables QSqlQuery *query, and QSqlDatabase baza. I'm allocating memory for QSqlQuery with:
    query = new QSqlQuery(baza);
    I'm able to open database. But when I call a slot that id doing query in consol i still see
    database not open
    Here is code of my slot:
    Qt Code:
    1. if(!query->prepare("INSERT into urzadzenia(nazwa, opis) VALUES (:nazwa, :opis))")) std::cerr<<"\nError in prepering query\n";
    2. query->bindValue(":nazwa", naz);
    3. query->bindValue(":opis", opis);
    4. if(bazaDanych.isOpen()){
    5. std::cout<<"DB open\t";
    6. std::cout<<"query.exec()"<<query->exec()<<'\n';
    7. }
    8. else{
    9. std::cout<<"DB not open";
    10. }
    To copy to clipboard, switch view to plain text mode 
    And i console I can see:
    QSqlQuery::prepare: database not open
    Error in prepering query
    DB open query.exec() 0
    Any ideas what is wrong?
    Last edited by xkazielx; 20th July 2009 at 13:28. Reason: updated contents

  5. #5
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: [QSqlQuery] Problem with not open database

    Maybe the "into" should be capitalized and you need to call the QString constructor.

    Qt Code:
    1. if(!query->prepare(QString("INSERT INTO urzadzenia(nazwa, opis) VALUES (:nazwa, :opis))"))) std::cerr<<"\nError in prepering query\n";
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [QSqlQuery] Problem with not open database

    coud you provide a minimal compilaple example which reproduces a problem?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Jul 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: [QSqlQuery] Problem with not open database

    i have the same problem.
    im using a SQLITE3 database. i didnt create the table contained from my app, i've crate it outside with another program (sqlite browser).


    here is the first fragment of my code, below:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QSqlDriver>
    4. #include <QSqlDatabase>
    5. #include <QSqlQuery>
    6. #include <QSqlQueryModel>
    7. #include <QSqlTableModel>
    8. #include <QMessageBox>
    9. #include <QSqlError>
    10. #include <QDate>
    11. QString movistar[6]={"10 Bs","12 Bs","20 Bs","25 Bs","40 Bs","60 Bs"};
    12. QString movilnet[4]={"15 Bs","25 Bs","40 Bs","60 Bs"};
    13. int mov=6,movi=4;
    14.  
    15. MainWindow::MainWindow(QWidget *parent) :
    16. QMainWindow(parent),
    17. ui(new Ui::MainWindow)
    18. {
    19. ui->setupUi(this);
    20. //deshabilitar boton maximizar
    21. // this->setWindowFlags(Qt::WindowMinimizeButtonHint);
    22. //create the database
    23. QSqlDatabase bd=QSqlDatabase::addDatabase("QSQLITE");
    24. //set database name
    25. bd.setDatabaseName("C:/registro de ventas.db3");
    26. bd.setPassword("120590");
    27. bd.setHostName("localhost");
    28.  
    29. if(!bd.open())
    30. {
    31. MessageBox::critical(this,"error","error al abrir base de datos");
    32.  
    33.  
    34. }
    35. else
    36. { MessageBox::about(this,"error","base de datos abierta");
    37.  
    38. }
    39. //inicializando montos....
    40. for(int i=0;i<6;i++)
    41. ui->monto->addItem(movistar[i]);
    42.  
    43. ui->monto->setCurrentIndex(0);
    44.  
    45. calcular();
    46. //-------------------
    47. }
    To copy to clipboard, switch view to plain text mode 

    in this first segment of the code, i can realize that the conection to the database its succesfuly done.
    the problem come when i try to execute any query...insert, for example.

    Qt Code:
    1. void MainWindow::vender()
    2. venta objeto;
    3.  
    4. int indice=ui->monto->currentIndex();
    5. int tarjeta=0;
    6. int cant=ui->cantidad->value();
    7. float recibido=ui->efectivo->text().toFloat();
    8. objeto.cantidad=cant;
    9. if(ui->tipo->currentText()=="Movistar")
    10. { switch(indice)
    11. {
    12. case 0: tarjeta=10;break;
    13. case 1: tarjeta=12;break;
    14. case 2: tarjeta=20;break;
    15. case 3: tarjeta=25;break;
    16. case 4: tarjeta=40;break;
    17. case 5: tarjeta=60;break;
    18. }
    19. }
    20. if(ui->tipo->currentText()=="Movilnet")
    21. { switch(indice)
    22. {
    23. case 0: tarjeta=15;break;
    24. case 1: tarjeta=25;break;
    25. case 2: tarjeta=40;break;
    26. case 3: tarjeta=60;break;
    27. }
    28. }
    29. //calculando monto a cobrar, vuelto
    30. float acobrar=cant*tarjeta;
    31.  
    32. float vuelto2=recibido-(acobrar);
    33.  
    34. objeto.tipo=ui->tipo->currentText();
    35. //hora
    36. QDate d;
    37. QTime t;
    38. QString message;
    39. objeto.hora=t.currentTime();
    40. objeto.fecha=d.currentDate();
    41. //fecha
    42. objeto.acobrar=acobrar;
    43. objeto.recibido=recibido;
    44. objeto.vuelto=vuelto2;
    45.  
    46. if(registrar_BD(objeto)) //<------------------------------------------ here i call the function that will execute the query
    47. {m.setText("Registro exitoso");}
    48. else
    49. {message=sql.lastError().Text(); //< ---------------------- this will show me if any error happend.
    50. m.setText(message);
    51.  
    52. }
    53.  
    54. m.exec();
    55.  
    56. }
    57.  
    58. bool MainWindow::registrar_BD(venta objeto)//<---- this its the object that contain the data that i want to save
    59. { // and her i asingn constants values just for try
    60. return (sql.exec("INSERT INTO venta(tarjeta,monto,cantidad,efectivo_recibido,efectivo_devuelto) VALUES('digitel',20,1,40,20)"));
    61.  
    62. //return (sql.exec("insert into ventas (fecha,hora,tarjeta,monto,cantidad,efectivo_recibido,efectivo_devuelto)
    63. //"values(objeto.fecha,objeto.hora,objeto.tipo,objeto.monto,objeto.cantidad,objeto.recibido,objeto.vuelto)"));
    64.  
    65.  
    66. }
    To copy to clipboard, switch view to plain text mode 

    and this is the code of thel mainwindow.h:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include<mis_datos.h>
    6. #include <QSqlQuery>
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21. QImage Imagen;
    22. private slots:
    23. void calcular();
    24. void comprobarTipo();
    25. void mostrarimg();
    26. void cargarimg(int,int);
    27. void vender();
    28. bool registrar_BD(venta);
    29. };
    30.  
    31. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    when i execute my app, just when i push the "vender" button, this is what i got:

    i have to say that i've tried to compile the libraries of sqlite with the commands below:
    configure -qt-sql-sqlite para habilitar las librerias sqlite
    and configure -qmake32 ...or something like that...

    i've tried:
    put my app in the directory: Qt/2010.04/qt/bin
    copy and pste the dll's of sqlite from Qt/2010.04/qt/bin and sqldrivers to my app directory
    here is a rar whith the proyect (i know that it have so many sintaxis errors and logicals errors, but it doesnt matter in the end):
    http://www.fileden.com/files/2007/6/..._tarjetas2.rar

    some details

    * the file "registro de ventas.db3" that its inside the rar, is the database that must be in C partition. well, in my code i put it there, you can move it for use it. but that is the file that have to be used.

    * my qt version is 4.6.3. con qt creator 2.0

    i hope some one of u give me the answer!!!!! the succesfull answer!

    thanks so much friends!
    Last edited by spiderman3000p; 23rd July 2010 at 04:02. Reason: updated contents

  8. #8
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: [QSqlQuery] Problem with not open database

    From the docs:
    Warning: You must load the SQL driver and open the connection before a QSqlQuery is created.
    Works fine when the query is created in your "registrar_BD" method. You'll have to handle error messaging some other way if you do that though.

    HTH
    Last edited by norobro; 23rd July 2010 at 03:40.

  9. #9
    Join Date
    Jul 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [QSqlQuery] Problem with not open database

    i has solved that problem...thanks for your reply norobro.

  10. #10
    Join Date
    May 2013
    Location
    Schweiz
    Posts
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: [QSqlQuery] Problem with not open database

    Quote Originally Posted by spiderman3000p View Post
    i has solved that problem...thanks for your reply norobro.
    great, if you share your solution you might help others.
    thanks.

  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: [QSqlQuery] Problem with not open database

    You woke up a three year old thread to add that?

    Accessing databases has been done repeatedly in this forum and there are plenty of examples in the docs. If you have a specific question of your own then feel free to open a new thread, show what you have done, explain what you expected, what you got, what you have done to try to resolve the problem etc.

    Spiderman3000p simply had to follow norobro's hint that the routine works if the query object is created in the routine that tries to use it. Actually, it just needs to be created after the database is added.

  12. #12
    Join Date
    May 2013
    Location
    Schweiz
    Posts
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: [QSqlQuery] Problem with not open database

    Quote Originally Posted by ChrisW67 View Post
    You woke up a three year old thread to add that?
    Hi Chris,
    sorry for that, I saw the date of the post after I hit the reply button. Another lesson learned. I will be careful next time.

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.