Reputation: 1165
I'm new to C++/Qt and am trying to write to a SQLite database that I have created in the root directory of my project.
Here is one of the many sites I've been using for reference: http://www.java2s.com/Code/Cpp/Qt/ConnecttoSqliteanddoinsertdeleteupdateandselect.htm
I created a function that receives an argument of all the user entered values as an object. I would like to write these values that the user entered into my root directory database.
Here is the function that takes the database object and attempts to write them into my root level database "matrixics.db".
My Function:
#include <QtSql>
#include <QSqlDatabase>
#include <QtDebug>
#include <QFile>
#include <QMessageBox>
void db_connection::writeDBValues(db_config *dbInfo)
{
//connect to matrixics.db
QSqlDatabase matrixics = QSqlDatabase::addDatabase("QSQLITE", "MatrixICS");
//using absolute path doesn't work either
//matrixics.setDatabaseName("D:/Qt Projects/build-MatrixICS-Desktop_Qt_5_4_0_MinGW_32bit-Debug/matrixics.db");
matrixics.setDatabaseName("./matrixics.db");
if(!QFile::exists("./matrixics.db"))
{
QString failedMsg = tr("FAILED: Could not locate matrixics.db file");
QMessageBox::warning(this, tr("ERROR: Failed MatrixICS Database Connection!"), failedMsg);
}
else if (!matrixics.open())
{
QString failedMsg = tr("FAILED: ") + matrixics.lastError().text();
QMessageBox::warning(this, tr("ERROR: Failed to open matrixics.db!"), failedMsg);
}
else
{
//write db values
QSqlQuery qry;
if (m_connectionName == "localDb")
{
qry.prepare( "INSERT INTO settings (local_db_type, local_db_host, local_db_port, local_db_user, local_db_pass) VALUES (?, ?, ?, ?, ?)" );
}
else if (m_connectionName == "remoteDb")
{
qry.prepare( "INSERT INTO settings (remote_db_type, remote_db_host, remote_db_port, remote_db_user, remote_db_pass) VALUES (?, ?, ?, ?, ?)" );
}
//bind all values
qry.addBindValue(dbInfo->m_db_type);
qry.addBindValue(dbInfo->m_hostname);
qry.addBindValue(dbInfo->m_port);
qry.addBindValue(dbInfo->m_db_user);
//encode user pass
//base64_encode is included in a globals.h fn not shown above
QString encodedPass = base64_encode(dbInfo->m_db_pass);
qry.addBindValue(encodedPass);
if(!qry.exec()) qDebug() << qry.lastError();
matrixics.close();
}
QSqlDatabase::removeDatabase("MatrixICS");
}
The Error:
QSqlQuery::prepare: database not open
QSqlError("", "Driver not loaded", "Driver not loaded")
QSqlDatabasePrivate::removeDatabase: connection 'MatrixICS' is still in use, all queries will cease to work.
My Question:
How is it possible for the script to make it down to the QSqlQuery::prepare
function without the database being open? I would think that when I perform if (!matrixics.open())
the script would throw my "Failed to open" message if the database was not open. However, it is not, so logic should dictate that the database is in fact open, yet I receive QSqlQuery::prepare: database not open
.
Upvotes: 0
Views: 2236
Reputation: 27518
Your QSqlQuery
object must be constructed with a reference to the QSqlDatabase
. In other words, make that line
QSqlQuery qry(matrixics);
See QSqlQuery::QSqlQuery
documentation.
Upvotes: 1