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();
Related
I have a folder where i have a many databases. Some times may be deleted or added database to the folder.
So I use QTimer and read all databases.
It is a my code:
this->timer = new QTimer(this);
this->timer->setInterval(15000);
connect(this->timer, &QTimer::timeout, this, [=]() {
QString path = "C:\\Users\\User\\Desktop\\DAXI SMS SENDER\\SMSSenderAllBASE";
//QString path = qApp->applicationDirPath() + "\\SMSSenderAllBASE";
QDir recoredDir(path);
QStringList allFiles = recoredDir.entryList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst);
for (int i = 0; i < allFiles.size(); i++) {
QString fullPath = path + "\\" + allFiles[i];
QString connectionName = allFiles[i];
connectionName = connectionName.remove(connectionName.size() - 4, 4);
QSqlDatabase db = QSqlDatabase::addDatabase("QIBASE", connectionName);
db.setDatabaseName(fullPath);
db.setHostName("localhost");
db.setPort(3050);
db.setUserName("SYSDBA");
db.setPassword("masterkey");
thrdHelperSendSMS *help = new thrdHelperSendSMS(db, this);
connect(help, &thrdHelperSendSMS::helperFinished, this, [=](QString connectionName){
QSqlDatabase t_db = QSqlDatabase::database(connectionName);
t_db.close();
QSqlDatabase::removeDatabase(connectionName);
delete help;
});
help->run();
}
});
this->timer->start();
Yes I'm sure that the helperFinished signal will happen and this time I will not have any connection with this base.
EDIT:
If i remove
thrdHelperSendSMS *help = new thrdHelperSendSMS(db, this);
connect(help, &thrdHelperSendSMS::helperFinished, this, [=](QString connectionName){
QSqlDatabase t_db = QSqlDatabase::database(connectionName);
t_db.close();
QSqlDatabase::removeDatabase(connectionName);
delete help;
});
help->run();
example:
for (int i = 0; i < allFiles.size(); i++) {
QString fullPath = path + "\\" + allFiles[i];
QString connectionName = allFiles[i];
connectionName = connectionName.remove(connectionName.size() - 4, 4);
QSqlDatabase db = QSqlDatabase::addDatabase("QIBASE", connectionName);
db.setDatabaseName(fullPath);
db.setHostName("localhost");
db.setPort(3050);
db.setUserName("SYSDBA");
db.setPassword("masterkey");
QSqlDatabase::removeDatabase(connectionName);
}
I have the same error.
You don't use removeDatabase() correctly. The object of SqlDatabase needs to go out of scope first. See documentation.
Wrong use
QSqlDatabase db = QSqlDatabase::database("sales");
QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
QSqlDatabase::removeDatabase("sales"); // will output a warning
Correct use
{
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
In the second example db will go out of scope after } and you will no longer see the error message QSqlDatabasePrivate::removeDatabase: connection 'myConnectionName' is still in use, all queries will cease to work
Please read the documentation carefully. The database stuff is sensible and every line needs to be checked carefully.
Also you have missing db.close(); - It makes sense to close the database before you remove it.
#user3606329's answer is right, but I add this possibility:
QSqlDatabase db = QSqlDatabase::database("sales");
{
QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
//use query
}
db = QSqlDatabase();
QSqlDatabase::removeDatabase("sales");
You can use std::swap to force it destruct on demand.
I basically use BOOST_SCOPE_EXIT for cases when want to call a function on scope exit including not expected exit like through an exception throw.
#include <boost/scope_exit.hpp>
{
QSqlDatabase db;
BOOST_SCOPE_EXIT(this_, &db) { // by reference, otherwise it will copy a stack object
// access object here through the this_ instead of this ...
if (db.isOpen()) {
db.close(); // closing if not yet closed before connection remove
}
std::swap(db, QSqlDatabase{}); // destruct via swap
// CAUTION:
// From this point you must not call to `QSqlDatabase::database("MYDB", ...)`, otherwise it will return another constructed object!
//
QSqlDatabase::removeDatabase("MYDB");
} BOOST_SCOPE_EXIT_END
// ui change from here ...
// accomplish last ui changes before long blocking operation
qApp->processEvents();
db = QSqlDatabase::addDatabase("...", "MYDB");
// database access from here ...
}
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "conn_name");
db.open();
if (db.open())
{
qDebug()<<"DataBase is Open";
}
else
{
qDebug()<<"DataBase is Not Open";
}
QSqlQueryModel * model = new QSqlQueryModel();
QSqlQuery query(QSqlDatabase::database("conn_name"));
query.exec("SMTHING")
if (query.exec())
{
while (query.next())
{
ui->QTableView->setModel(model);
model->setHeaderData(2, Qt::Horizontal, QObject::tr("????"));
}
}
db.close();
QSqlDatabase::removeDatabase("conn_name");
Here is my code
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.
QSqlDatabase db;
DBCONNECTION( QString conName)
{
db.addDatabase("QMYSQL",conName);
db.setDatabaseName("mitsubishi");
db.setHostName("localhost");
db.setUserName("root");
db.setPassword("");
qDebug()<<db.connectionName()<<conName;
}
db.connectionName returns empty string
but conName returns "string"
whats the problem?
and while executing query driver is not loaded
QSqlDatabase::addDatabase is a static function which returns a QSqlDatabase object. So it is not doing anything to your existing QSqlDatabase object. How you should use it:
db = QSqlDatabase::addDatabase("QMYSQL", conName);
I am trying to connect to a Azure SQL Server database with Qt on Linux but I have not could make it. I tried some like this: Connection to SQL Server with qt but the connection is never opened.
My code is so simple:
QString connectionTemplate = "DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;";
QString connectionString = connectionTemplate.arg("tcp:my-database.database.windows.net,1433").arg("my-database");
for(int i = 0; i < QSqlDatabase::drivers().size(); i++) {
qDebug() << QSqlDatabase::drivers().at(i);
}
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName(connectionString);
db.setUserName("user#my-database");
db.setPassword("My password");
//db.setConnectOptions("Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;");
bool ok = false;
try {
ok = db.open();
} catch(QException ex) {
qDebug() << ex.what();
}
qDebug("%s=%d", "conexión abierta", ok);
QSqlQueryModel *model = new QSqlQueryModel;
QString query = "SELECT 1 AS test_col";
model->setQuery(query, db);
db.close();
I already have the QODB and QODBC3 drivers so I don't know why I am unable to make the connection.
Is some related to driver, Qt, Azure or similar?
This is what I am doing and it works perfect for me:
Note: connName is the connection name on which I am opening the database.
QString connectionString = "Driver={ODBC Driver 13 for SQL Server};"
"Server=tcp:xxx.database.windows.net,1433;"
"Database=ABC;"
"Uid=aaa#xxx;"
"Pwd=***;"
"Encrypt=yes;"
"TrustServerCertificate=no;"
"Connection Timeout=30;";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", connName);
db.setDatabaseName(connectionString);
if (db.open())
{
return true;
}
else
{
QString error = db.lastError().text();
return false;
}
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();