QT SQLITE Database wont open - c++

I am having an extremely annoying problem and it doesn't make sense to me whats wrong. I am creating a multiple form ATM app but for some reason my DB is saying its open in the app but in the output it is throwing the error: QSqlQuery::exec: database not open
Here is the mainmenu.cpp file where I create the first instance of the DB
mainMenu::mainMenu(QWidget *parent) :QMainWindow(parent),ui(newUi::mainMenu)
{
ui->setupUi(this);
atmDB = QSqlDatabase::addDatabase("QSQLITE","myConnection");
atmDB.setDatabaseName(Path_to_DB);
QFileInfo checkFile(Path_to_DB);
}
Here in the login.cpp file it displays on the screen "Database connected"
login::login(QWidget *parent) :QDialog(parent),ui(new Ui::login)
{
ui->setupUi(this);
QSqlDatabase logindb;
logindb = QSqlDatabase::database("myConnection",true);
bool x = logindb.open();
if(!x){
ui->loginStatusL->setText("No connection to log-in database!");
}
else
ui->loginStatusL->setText("Database connected!");
}
But when the user pushes the button to login it it throws the SqlQuery error,
void login::on_loginLoginB_clicked()
{
QSqlDatabase loginB;
loginB = QSqlDatabase::database("myConnection",true); //open database
QString email, password;
email = ui->loginEmailbox->text();
password = ui->loginPassbox->text();
pubEmail = email;
if(!loginB.isOpen()){
ui->loginStatusL->setText("Connection to database lost");
return;
}
QSqlQuery qry;
if(qry.exec("SELECT * FROM userRecords WHERE email = '"+email+"' and password='"+password+"'")){
int count = 0;
while(qry.next()){
count++;
}
if(count==1){
userMenu usermenu;
atmDB.close();
usermenu.setModal(true);
usermenu.exec();
}
else
ui->loginStatusL->setText("Login failed. Email or password incorrect.");
}
}
I also have the same problem in my register.cpp file and it follows the same logic.

If you're looking to just know why wouldn't the QSqlQuery::exec() work, that's because you haven't told it anything about the database that it should run the query on.
The initialization should look more like:
QSqlDatabase loginB;
//code omitted...
QSqlQuery qry(loginDB); //Tell QSqlQuery which database to work with
//call qry.exe() here
I'd bet that it's the same problem in your register.cpp.

Related

How can I change the timing for signals and slot in QT?

I am creating a program that manages student information. Currently I have a tableview and I have an add button, when I click on the add button, a new dialog pops up, prompting users to add a new student. My intentions are that I create a signal and slot connection to my dialog so that whenever the ok button is pressed, my tableview would refresh (I am using SQLITE). However, the problem right now is that it seems to me that the database is being updated after I call my refreshwindow function, so when I call my refreshwindow function, the database hasn't been updated yet. I am not sure if this is the problem forsure but this is what I think.
Below are some codes:
When I click on the add button
void viewStudents::on_addStudent_clicked()
{
studentWindow = new studentManagement(username,this);
QObject::connect(studentWindow,SIGNAL(accepted()),this, SLOT(refreshwindow()));
studentWindow->show();
}
my refreshwindow function
void viewStudents::refreshwindow()
{
QSqlQueryModel *modal = new QSqlQueryModel();
QSqlDatabase tempdb = QSqlDatabase::addDatabase("QSQLITE");
tempdb.setDatabaseName("accounts.db");
if(tempdb.open()){
QSqlQuery tempquery;
tempquery.exec("SELECT firstname, lastname, DOB, Day_of_lessons, Start_date, Price_per_lesson, Length_of_lessons from studentList WHERE teacher = '"+username+"';");
modal->setQuery(tempquery);
ui->tableView->setModel(modal);
ui->tableView->resizeColumnsToContents();
ui->tableView->resizeRowsToContents();
tempdb.close();
}
else{
QMessageBox::warning(this,"Error","Something unexpected has happened.");
}
}
my dialog for adding students
studentManagement::studentManagement(QString username, QWidget *parent) :
QDialog(parent),
ui(new Ui::studentManagement)
{
this->username = username;
ui->setupUi(this);
QFont information_font = ui->informationLabel->font();
information_font.setPointSize(14);
information_font.setBold(true);
ui->informationLabel->setFont(information_font);
ui->startdate->setMinimumDate(QDate::currentDate());
ui->dayOfLessonsBox->addItem("Monday");
ui->dayOfLessonsBox->addItem("Tuesday");
ui->dayOfLessonsBox->addItem("Wednesday");
ui->dayOfLessonsBox->addItem("Thursday");
ui->dayOfLessonsBox->addItem("Friday");
ui->dayOfLessonsBox->addItem("Saturday");
ui->dayOfLessonsBox->addItem("Sunday");
}
studentManagement::~studentManagement()
{
delete ui;
}
void studentManagement::on_buttonBox_accepted()
{
QString firstname = ui->firstname->text();
QString lastname = ui->lastname->text();
QString DOB = ui->dateofbirth->text();
QString dayOfLessons = ui->dayOfLessonsBox->currentText();
QString startdate = ui->startdate->text();
QString pricing = ui->pricing->text();
QString lengthoflessons = ui->lengthoflessons->text();
QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("accounts.db");
if(!mydb.open())QMessageBox::warning(this,"File Not Found Error", "The database file cannot be find.");
else{
QSqlQuery query;
if(query.exec("INSERT INTO studentList VALUES('"+firstname+"', '"+lastname+"', '"+DOB+"', '"+dayOfLessons+"', '"+startdate+"', '"+pricing+"', '"+lengthoflessons+"', '"+username+"');")){
mydb.close();
}
}
}
If someone could help me out a little or give me some suggestions on where to look I would really appreciate it!
First of all you should have used your connect statement in constructors or every time on_addStudent_clicked() is being called a new connect will be executed which does not formulate a bug but it is completely unnecessary.
Secondly, when we are adding a record to a list we want to make sure list is being refreshed right away, So it need to be right after the insert statement.
Thirdly, Qt is very much intelligent to find the implicit connects that you want to create, if you are creating a function with the general form of on_something_someevent() then it will automatically look for object/class (of type QObject) named something and connects its signal which is someevent to this and on_something_someevent() as the slot. As a result writing a function like thisobject::on_something_someevent(){} will automatically rendered into :
QObject::connect(something, SIGNAL(someevent()), this, SLOT(on_something_someevent()));
which is very convenient, but it could tries to create an unwanted connect or at least raise a warning or error.
Finally, do not forget to define your slot with public slot: in your header file. Public part is necessary or you would get access violation error since Qt framework can not call your slot function (from another object).
Here it is the corrected form of your code that I think could do the job (I didn't have your .ui or .h files neither the full source of your both classes and your main() function, so please consider this as a partial source which I hope that conveys the gist)
void viewStudents::refreshwindow()
{
QSqlQueryModel modal = QSqlQueryModel::QSqlQueryModel();
QSqlDatabase tempdb = QSqlDatabase::addDatabase("QSQLITE");
tempdb.setDatabaseName("accounts.db");
if (tempdb.open()) {
QSqlQuery tempquery;
tempquery.exec("SELECT firstname, lastname, DOB, Day_of_lessons, Start_date, Price_per_lesson, Length_of_lessons from studentList WHERE teacher = '" + username + "';");
modal->setQuery(tempquery);
ui->tableView->setModel(modal);
ui->tableView->resizeColumnsToContents();
ui->tableView->resizeRowsToContents();
tempdb.close();
}
else {
QMessageBox::warning(this, "Error", "Something unexpected has happened.");
}
}
studentManagement::studentManagement(QString username, QWidget *parent) :
QDialog(parent),
ui(new Ui::studentManagement)
{
this->username = username;
ui->setupUi(this);
QFont information_font = ui->informationLabel->font();
information_font.setPointSize(14);
information_font.setBold(true);
ui->informationLabel->setFont(information_font);
ui->startdate->setMinimumDate(QDate::currentDate());
ui->dayOfLessonsBox->addItem("Monday");
ui->dayOfLessonsBox->addItem("Tuesday");
ui->dayOfLessonsBox->addItem("Wednesday");
ui->dayOfLessonsBox->addItem("Thursday");
ui->dayOfLessonsBox->addItem("Friday");
ui->dayOfLessonsBox->addItem("Saturday");
ui->dayOfLessonsBox->addItem("Sunday");
// This connect statment could be quit unnecessary as Qt will create it automatically
// Since you have created the function name as it is expected by Qt (on_class_event)
QObject::connect(addStudent, SIGNAL(clicked()), this, SLOT(on_addStudent_clicked()));
}
studentManagement::~studentManagement()
{
delete ui;
}
void studentManagement::on_addStudent_clicked()
{
QString firstname = ui->firstname->text();
QString lastname = ui->lastname->text();
QString DOB = ui->dateofbirth->text();
QString dayOfLessons = ui->dayOfLessonsBox->currentText();
QString startdate = ui->startdate->text();
QString pricing = ui->pricing->text();
QString lengthoflessons = ui->lengthoflessons->text();
QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("accounts.db");
if (!mydb.open())
{
QMessageBox::warning(this, "File Not Found Error", "The database file cannot be find.");
}
else
{
QSqlQuery query;
if (query.exec("INSERT INTO studentList VALUES('" + firstname + "', '" + lastname + "', '" + DOB + "', '" + dayOfLessons + "', '" + startdate + "', '" + pricing + "', '" + lengthoflessons + "', '" + username + "');"))
mydb.close();
}
studentWindow = new studentManagement(username, this);
studentWindow->show();
viewStudents::refreshwindow();
// OR
// define refreshwindow as a public slot and emit a signal from here
emit(viewStudents::refreshwindow());
}

connection 'qt_sql_default_connection' is still in use, all queries will cease to work

I've made separate functions for open and close connection.But it wont let me to add new record on new form.
this is login header file.
public:
QSqlDatabase mydb;
void connClose()
{
//QString connection;
//connection = mydb.connectionName();
mydb.close();
//mydb.removeDatabase(connection);
mydb.removeDatabase(mydb.connectionName());
}
bool connOpen()
{
mydb=QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("./Poem.db");
if(mydb.open())
{
return true;
}
else if(!mydb.open())
{
return false;
}
}
this is the other form add button :
QString Title,Group,Poem;
Title = ui->lineEdit->text();
Group = ui->label_2->text();
Poem = ui->textEdit->toPlainText();
MainWindow mainwindow;
mainwindow.connOpen();
QSqlQuery * qry1 = new QSqlQuery(mainwindow.mydb);
qry1->prepare("insert into Poems(Title,Poem,Group) values ('"+Title+"','"+Poem+"','"+Group+"')");
if(qry1->exec())
{
QMessageBox::critical(this,tr("درج شعر جدید"),tr("شعر اضافه شد"));
mainwindow.connClose();
}
I get this errors :
duplicate connection name 'qt_sql_default_connection', old connection removed.
connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
You are committing exactly what Qt QSqlDatabase Documentation warns about:
Warning: There should be no open queries on the database connection
when this function is called
...
// WRONG
QSqlDatabase db = QSqlDatabase::database("sales");
QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
QSqlDatabase::removeDatabase("sales"); // will output a warning
// "db" is now a dangling invalid database connection, // "query"
contains an invalid result set
and the correct is:
{
QSqlDatabase db = QSqlDatabase::database("sales");
QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
}
// Both "db" and "query" are destroyed because they are out of scope
QSqlDatabase::removeDatabase("sales"); // correct
So in your case you execute query qry1 and remove the database within same scope (i.e before qry1 goes out of scope), you should modify your code to make sure qry1 is executed and gets destroyed / goes out of scope / before deleting the database. Try this:
{
QSqlQuery * qry1 = new QSqlQuery(mainwindow.mydb);
qry1->prepare("insert into Poems(Title,Poem,Group) values ('"+Title+"','"+Poem+"','"+Group+"')");
if(qry1->exec())
{
QMessageBox::critical(this,tr("درج شعر جدید"),tr("شعر اضافه شد"));
}
}
mainwindow.connClose();

QSqlDatabase can't be removed - Queries still active

I started with c++ QT recently.
I created class "ControllerOfDB" to hold pointer to my QSqlDatabase and few functions (to make inserts/selects).
Example select function:
QList<data1> GetData1()
{
QList<data1> output;
if(!dataBase->isOpen())
dataBase->open();
if(dataBase->isOpen())
{
QSqlQuery* query = new QSqlQuery(*dataBase);
query->prepare("SELECT * FROM table1");
if(query->exec())
while (query->next())
{
output.append( *(new data1(
query->value(0).toInt(),
query->value(1).toString(),
query->value(2).toInt(),
query->value(3).toInt(),
query->value(4).toInt(),
)) );
}
query->clear();
query->finish();
delete query;
}
return output;
}
It was all working fine, until i had to add possibility to change db or login as different user. I modified Connect and Disconnect function. After few iterrations, this is what i get:
void Connect()
{
dataBase = new QSqlDatabase(QSqlDatabase::addDatabase("QPSQL", "Main"));
dataBase->setHostName(hostName);
dataBase->setPort(port);
dataBase->setDatabaseName(dbName);
dataBase->setUserName(userName);
dataBase->setPassword(userPass);
if(!dataBase->isOpen())
dataBase->open();
}
void Disconnect()
{
if(dataBase != NULL)
if(dataBase->isOpen()){
dataBase->close();
dataBase->removeDatabase("Main");
dataBase = NULL;
}
}
hostName, port, dbName, userName and userPass are also attributes of that class.
public:
QSqlDatabase *dataBase;
QString hostName;
int port;
QString dbName;
QString userName;
QString userPass;
whenever i try to diconnect and create new connection i recieve warnings/errors about queries of my current connections - even if i only "connected" ( = used function Connect() ).
I already saw few similar topics and documentation for it, saying i have to remove queries from scope, but at this point i don't know how.
when you look at QSqlDatabase document, the method removeDatabase is static, so your method Disconnect logic should be like:
void Disconnect()
{
if(dataBase != NULL) {
if(dataBase->isOpen()){
dataBase->close();
}
delete dataBase;
dataBase = NULL;
}
QSqlDatabase::removeDatabase("Main");
}

Handling QSqlDatabase connections

What is the correct way to handle connections for QSqlDatabase?
In my program I am doing it this way:
DatabaseConnector *databaseConnector = 0;
try
{
databaseConnector = new DatabaseConnector();
//Do stuff...
delete databaseConnector;
}
catch(QString e)
{
delete databaseConnector;
QMessageBox::information(this,"Error",e);
}
databaseConnector.h
#ifndef DATABASECONNECTOR_H
#define DATABASECONNECTOR_H
#include <QtSql>
class DatabaseConnector
{
public:
DatabaseConnector();
DatabaseConnector(QString hostname, QString database, QString user, QString password);
~DatabaseConnector();
private:
QSqlDatabase db;
};
#endif // DATABASECONNECTOR_H
databaseconnector.cpp
#include "databaseconnector.h"
#include <QString>
DatabaseConnector::DatabaseConnector()
{
QSettings settings;
db = QSqlDatabase::addDatabase("QIBASE");
db.setHostName(settings.value("db/host").toString());
db.setDatabaseName(settings.value("db/name").toString());
db.setUserName(settings.value("db/user").toString());
db.setPassword(settings.value("db/pass").toString());
if(!db.open())
{
QString databaseConnectionError = db.lastError().text();
throw databaseConnectionError;
}
}
DatabaseConnector::DatabaseConnector(QString hostname, QString database, QString user, QString password)
{
db = QSqlDatabase::addDatabase("QIBASE");
db.setHostName(hostname);
db.setDatabaseName(database);
db.setUserName(user);
db.setPassword(password);
if(!db.open())
{
QString databaseConnectionError = db.lastError().text();
throw databaseConnectionError;
}
}
DatabaseConnector::~DatabaseConnector()
{
db.close();
}
I'm getting error even if I use QSqlDatabase::removeDatabase(db.connectionName());
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
Normally you don’t need to open the database connection more than once within your application.
When adding a database, you can name the connection :
QSqlDatabase::addDatabase( "QIBASE", "MyDBConnectionName" );
You can use the name to query for the connection :
if( QSqlDatabase::contains( "MyDBConnectionName" ) )
{
QSqlDatabase db = QSqlDatabase::database( "MyDBConnectionName" );
//Do stuff...
}
else
{
// connection not found, do something
}
Also notice that before calling QSqlDatabase::removeDatabase you should disconnect your database :
db.close();
QSqlDatabase::removeDatabase("MyDBConnectionName");
In order to add a new database, you need to give it a name. If you do not give a unique name, the default database is re-used. This is documented in the class reference.
Try:
db = QSqlDatabase::addDatabase("QIBASE", databaseName);
In main app:
QSqlDatabase db =QSqlDatabase::addDatabase( "QSQLITE");
in second app:
QSqlDatabase db2 =QSqlDatabase::database();

SQLite remove database error

I have widget which connects to database:
Widget::Widget(QWidget *parent)
{
QString databaseName = "name";
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(databaseName);
db.setHostName("localhost");
if(!db.open())
qDebug()<<"ret error";
}
Now I want to delete database connection after widget close (currently I get warnings like: QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection is still in use...). I' ve read some topics and tried to evaluate some solution from them but none works for me. My code:
void Widget::closeEvent(QCloseEvent *e)
{
QSqlDatabase db = QSqlDatabase::database();
QString connection = db.connectionName();
db.close();
QSqlDatabase::removeDatabase(connection);
qDebug()<<"error: "<<db.lastError().text();
}
Error I get is: Driver not loaded Driver not loaded
What is the correct way to do this?
Edit:
another method:
void Widget::someMethod()
{
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
query.exec("some query");
}
Try giving a connection name parameter(2nd parameter) in addDatabase() , that should solve your problem.
Here is the modified code you could try:
Widget::Widget(QWidget *parent)
{
QString databaseName = "name";
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "test_db_connection" );
db.setDatabaseName(databaseName);
db.setHostName("localhost");
if(!db.open())
qDebug()<<"ret error";
}
Here is a complete working code from my machine for sqlite database which you can use as reference:
local_db = QSqlDatabase::addDatabase("QSQLITE","localdb");
local_db.setDatabaseName("localdb.sqlite");
local_db_query = QSqlQuery(local_db);
local_db_query.prepare( "SELECT * FROM sample_table" );
local_db_query.exec();