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:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlDriver>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSqlTableModel>
#include <QMessageBox>
#include <QSqlError>
#include <QDate>
QString movistar
[6]={"10 Bs",
"12 Bs",
"20 Bs",
"25 Bs",
"40 Bs",
"60 Bs"};
QString movilnet
[4]={"15 Bs",
"25 Bs",
"40 Bs",
"60 Bs"};
int mov=6,movi=4;
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
//deshabilitar boton maximizar
// this->setWindowFlags(Qt::WindowMinimizeButtonHint);
//create the database
//set database name
bd.setDatabaseName("C:/registro de ventas.db3");
bd.setPassword("120590");
bd.setHostName("localhost");
if(!bd.open())
{
MessageBox::critical(this,"error","error al abrir base de datos");
}
else
{ MessageBox::about(this,"error","base de datos abierta");
}
//inicializando montos....
for(int i=0;i<6;i++)
ui->monto->addItem(movistar[i]);
ui->monto->setCurrentIndex(0);
calcular();
//-------------------
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlDriver>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSqlTableModel>
#include <QMessageBox>
#include <QSqlError>
#include <QDate>
QString movistar[6]={"10 Bs","12 Bs","20 Bs","25 Bs","40 Bs","60 Bs"};
QString movilnet[4]={"15 Bs","25 Bs","40 Bs","60 Bs"};
QSqlQuery sql;
int mov=6,movi=4;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//deshabilitar boton maximizar
// this->setWindowFlags(Qt::WindowMinimizeButtonHint);
//create the database
QSqlDatabase bd=QSqlDatabase::addDatabase("QSQLITE");
//set database name
bd.setDatabaseName("C:/registro de ventas.db3");
bd.setPassword("120590");
bd.setHostName("localhost");
if(!bd.open())
{
MessageBox::critical(this,"error","error al abrir base de datos");
}
else
{ MessageBox::about(this,"error","base de datos abierta");
}
//inicializando montos....
for(int i=0;i<6;i++)
ui->monto->addItem(movistar[i]);
ui->monto->setCurrentIndex(0);
calcular();
//-------------------
}
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.
void MainWindow::vender()
venta objeto;
int indice=ui->monto->currentIndex();
int tarjeta=0;
int cant=ui->cantidad->value();
float recibido=ui->efectivo->text().toFloat();
objeto.cantidad=cant;
if(ui->tipo->currentText()=="Movistar")
{ switch(indice)
{
case 0: tarjeta=10;break;
case 1: tarjeta=12;break;
case 2: tarjeta=20;break;
case 3: tarjeta=25;break;
case 4: tarjeta=40;break;
case 5: tarjeta=60;break;
}
}
if(ui->tipo->currentText()=="Movilnet")
{ switch(indice)
{
case 0: tarjeta=15;break;
case 1: tarjeta=25;break;
case 2: tarjeta=40;break;
case 3: tarjeta=60;break;
}
}
//calculando monto a cobrar, vuelto
float acobrar=cant*tarjeta;
float vuelto2=recibido-(acobrar);
objeto.tipo=ui->tipo->currentText();
//hora
objeto.hora=t.currentTime();
objeto.fecha=d.currentDate();
//fecha
objeto.acobrar=acobrar;
objeto.recibido=recibido;
objeto.vuelto=vuelto2;
if(registrar_BD(objeto)) //<------------------------------------------ here i call the function that will execute the query
{m.setText("Registro exitoso");}
else
{message=sql.lastError().Text(); //< ---------------------- this will show me if any error happend.
m.setText(message);
}
m.exec();
}
bool MainWindow::registrar_BD(venta objeto)//<---- this its the object that contain the data that i want to save
{ // and her i asingn constants values just for try
return (sql.exec("INSERT INTO venta(tarjeta,monto,cantidad,efectivo_recibido,efectivo_devuelto) VALUES('digitel',20,1,40,20)"));
//return (sql.exec("insert into ventas (fecha,hora,tarjeta,monto,cantidad,efectivo_recibido,efectivo_devuelto)
//"values(objeto.fecha,objeto.hora,objeto.tipo,objeto.monto,objeto.cantidad,objeto.recibido,objeto.vuelto)"));
}
void MainWindow::vender()
{QMessageBox m;
venta objeto;
int indice=ui->monto->currentIndex();
int tarjeta=0;
int cant=ui->cantidad->value();
float recibido=ui->efectivo->text().toFloat();
objeto.cantidad=cant;
if(ui->tipo->currentText()=="Movistar")
{ switch(indice)
{
case 0: tarjeta=10;break;
case 1: tarjeta=12;break;
case 2: tarjeta=20;break;
case 3: tarjeta=25;break;
case 4: tarjeta=40;break;
case 5: tarjeta=60;break;
}
}
if(ui->tipo->currentText()=="Movilnet")
{ switch(indice)
{
case 0: tarjeta=15;break;
case 1: tarjeta=25;break;
case 2: tarjeta=40;break;
case 3: tarjeta=60;break;
}
}
//calculando monto a cobrar, vuelto
float acobrar=cant*tarjeta;
float vuelto2=recibido-(acobrar);
objeto.tipo=ui->tipo->currentText();
//hora
QDate d;
QTime t;
QString message;
objeto.hora=t.currentTime();
objeto.fecha=d.currentDate();
//fecha
objeto.acobrar=acobrar;
objeto.recibido=recibido;
objeto.vuelto=vuelto2;
if(registrar_BD(objeto)) //<------------------------------------------ here i call the function that will execute the query
{m.setText("Registro exitoso");}
else
{message=sql.lastError().Text(); //< ---------------------- this will show me if any error happend.
m.setText(message);
}
m.exec();
}
bool MainWindow::registrar_BD(venta objeto)//<---- this its the object that contain the data that i want to save
{ // and her i asingn constants values just for try
return (sql.exec("INSERT INTO venta(tarjeta,monto,cantidad,efectivo_recibido,efectivo_devuelto) VALUES('digitel',20,1,40,20)"));
//return (sql.exec("insert into ventas (fecha,hora,tarjeta,monto,cantidad,efectivo_recibido,efectivo_devuelto)
//"values(objeto.fecha,objeto.hora,objeto.tipo,objeto.monto,objeto.cantidad,objeto.recibido,objeto.vuelto)"));
}
To copy to clipboard, switch view to plain text mode
and this is the code of thel mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<mis_datos.h>
#include <QSqlQuery>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void calcular();
void comprobarTipo();
void mostrarimg();
void cargarimg(int,int);
void vender();
bool registrar_BD(venta);
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<mis_datos.h>
#include <QSqlQuery>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QImage Imagen;
private slots:
void calcular();
void comprobarTipo();
void mostrarimg();
void cargarimg(int,int);
void vender();
bool registrar_BD(venta);
};
#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!
Bookmarks