Sqlite and QtDialog Window
-
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.