Sqlite and QtDialog Window
-
I am trying to interact with an sql database through my dialog box. I make the connection, but when I try to run a query, nothing happens. I have a mainwindow source and header, a dialog source and header, and main source. I tried to follow a youtube tutorial, but he did it on his main window, so I figure I have includes or something in the wrong place. In the following order: car.h, car.cpp, mainwindow.h, mainwindow.cpp, main.cpp
@#ifndef CAR_H
#define CAR_H#include <QDialog>
#include <QtSql>
#include <QtDebug>
#include <QFileInfo>namespace Ui {
class Car;
}class Car : public QDialog
{
Q_OBJECTpublic:
explicit Car(QWidget *parent = 0);
~Car();private slots:
void on_pushButton_clicked();void on_pushButton_2_clicked();
private:
Ui::Car *ui;
QSqlDatabase mydb;
};#endif // CAR_H
@@#include "car.h"
#include "ui_car.h"
#include <QMessageBox>Car::Car(QWidget *parent) :
QDialog(parent),
ui(new Ui::Car)
{
ui->setupUi(this);
mydb = QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("RacingInfo.sqlite"); //Pathif(!mydb.isOpen()) ui->label_4->setText("Failed to open the database."); else ui->label_4->setText("Successfully opened database.");
}
Car::~Car()
{
delete ui;
}void Car::on_pushButton_clicked()
{
QString cartype,carnumber;
cartype=ui->lineEdit_type->text();
carnumber=ui->lineEdit_number->text();if(!mydb.isOpen()) { qDebug()<<"Failed to open database."; return; } QSqlQuery qry; if(qry.exec("select * from car where type='"+cartype +"' and number='"+carnumber+"'")) { int count=0; while(qry.next()) { count++; } if(count==1) ui->label_caradded->setText("Already Exists."); if(count>1) ui->label_caradded->setText("Duplicate Entries Already Found."); if(count<1) ui->label_caradded->setText("Successfully Added!"); }
}
void Car::on_pushButton_2_clicked()
{
Car::reject();
}@@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void on_pushButton_2_clicked();void on_exitButton_clicked(); void on_addcarButton_clicked();
private:
Ui::MainWindow *ui;
};#endif // MAINWINDOW_H
@@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "car.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_2_clicked()
{}
void MainWindow::on_exitButton_clicked()
{}
void MainWindow::on_addcarButton_clicked()
{
Car car;
car.setModal(true);
car.exec();
}
@@#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();return a.exec();
}
@ -
Hi and welcome to devnet,
You are checking that the query succeeds but you don't do anything if it fails. Did you verify what lastError returns ?
-
!http://i.imgur.com/cHlCccY.png(Error and proof that table exists)!
-
I seem to have .isOpen and .open used interchangeably throughout my code.... I have since moved all database creation to the mainwindow as it is initialized and I currently don't have the failed to open database error.
Will reply with whether or not query works when I get there.
But question:
- Do I need to include QtSql in all headers of windows that use SQL?
- How do I access my database from the other windows?
-
Don't include QtSql, just the headers of the class you are using.
Once the connection is done (if you are using the default connection) just use QSqlQuery and friends where needed
-
@ QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE", "connection-name");
db.setDatabaseName("RacingInfo.sqlite"); //Path@Is in my mainwindow.cpp initialization, I am unsure what exact code I should be using to access the database from another window/dialog. The second window is Modal if that makes any difference. I have tried many times with cut and paste code, but I continue to fail to connect to database.
-
Just use
@QSqlDatabase db = QSqlDatabase::database("connection-name");@
And reuse that db object in you query. Or if you only have one connection, don't set any name and you don't need to call QSqlDatabase::database. QSqlQuery for example uses the default connection if you don't specify otherwise
-
@void Car::on_pushButton_clicked()
{
QString cartype,carnumber;
cartype=ui->lineEdit_type->text();
carnumber=ui->lineEdit_number->text();QSqlDatabase db = QSqlDatabase::database(); QSqlQuery query(db); if(!db.isOpen()) { qDebug()<<"Failed to open database."; return; } else{ if(query.exec("SELECT * FROM Car")) ui->label_caradded->setText("Working"); else qDebug() << query.lastError(); }
}@
The database appears to be open in the second window as I have set an image to display if it is open when the window is initialized. I'm sorry, I am having such a hard time grasping this. But the database spits out, QSqlError(1, "Unable to execute statement", "no such table: Car") . No matter what table I put it or make, it doesn't change.
-
@ db.setDatabaseName("RacingInfo.sqlite"); //Path@
Where is "RacingInfo.sqlite" located ? It's a relative path so the application will try to open it in the same folder where the executable is located. I suspect that it's currently not the case.
-
@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "car.h"
#include "track.h"
#include "about.h"
#include "documentation.h"
#include "viewtable.h"
#include "racetype.h"
#include "addnew.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);QSqlDatabase db; db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("C:/Users/Andrew/Racing/RacingInfo.sqlite"); //Path if(!db.open()) { QPixmap pix(":/Resources/Images/Resources/Images/CircleRed.png"); ui->status->setPixmap(pix); qDebug()<<"Failed to open database."; return; } else{ QPixmap pix(":/Resources/Images/Resources/Images/CircleGreen.png"); ui->status->setPixmap(pix); qDebug()<<"I think database is open."; }
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_2_clicked()
{
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
if(!query.exec("SELECT * FROM Car"))
qDebug()<<query.executedQuery();
else
QMessageBox::information(this,tr("Database"),tr("Is working!");}@
!http://i.imgur.com/wFcmzPi.png(After clicking button, this is what qDebug spits out.)!
qDebug spits out "" every single time, I have multiple copies of my database in every possible location: where qt source files are, in the debug build folder, and one inside the debug folder inside that and I am not getting any info back. I noticed that there was a file called pRacingInfo.sqlite I don't know what that is. Following down my code, qDebug says my database is open when I initialize the window, but once I click the button I get the "".
So I cannot technically access it from my main window, let alone the others.
-
When you try to open a database with sqlite and the file doesn't exist it will be created if possible. That's why the opening always succeeds in your case.
So just ensure that you have the correct file at the correct place when opening it.