Results 1 to 12 of 12

Thread: [QSqlQuery] Problem with not open database

Threaded View

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

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.