Results 1 to 12 of 12

Thread: [QSqlQuery] Problem with not open database

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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.

  3. #3
    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.

  4. #4
    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.

  5. #5
    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.

  6. #6
    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
  •  
Qt is a trademark of The Qt Company.