Returning QSqlQueryModel for TableView
-
DB *db = new DB; QSqlQueryModel *model=new QSqlQueryModel; model->setQuery(db->SelectData()); ui->tableView->setModel(model); delete db;//closed and deleted db objects
db.cpp:
DB::~DB()//db destructor { database.close(); QSqlDatabase::removeDatabase("qt_sql_default_connection");//your dbname here }
example class.cpp:
{ DB *db=new DB; do something delete db; }
example class1.cpp:
DB *db=new DB; do something delete db;//we deleting db object and its call DB destructor
-
DB *db = new DB; QSqlQueryModel *model=new QSqlQueryModel; model->setQuery(db->SelectData()); ui->tableView->setModel(model); delete db;//closed and deleted db objects
db.cpp:
DB::~DB()//db destructor { database.close(); QSqlDatabase::removeDatabase("qt_sql_default_connection");//your dbname here }
example class.cpp:
{ DB *db=new DB; do something delete db; }
example class1.cpp:
DB *db=new DB; do something delete db;//we deleting db object and its call DB destructor
@Emre-MUTLU
constructor:DB::DB(const QString &path) { m_db = QSqlDatabase::addDatabase("QSQLITE"); m_db.setDatabaseName(path); if(!m_db.open()) qDebug() << "Error: connection with database failed"; else qDebug() << "Database: connection ok"; qDebug() << m_db.lastError().text(); }
destructor:
DB::~DB() { m_db.close(); m_db.removeDatabase("mypath"); //value of "path" variable in constructor }
and i got:
Database: connection ok "" QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed. Database: connection ok "" "" QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work. QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed. Database: connection ok "" QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed. Database: connection ok "" QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed. Database: connection ok "" ""
Every time new db oject instantiated with
DB db = new DB();
i got these errors. Im usingdelete db
like you recommended. -
DB::~DB() { m_db.close(); m_db.removeDatabase("qt_sql_default_connection"); }
-
@Emre-MUTLU
constructor:DB::DB(const QString &path) { m_db = QSqlDatabase::addDatabase("QSQLITE"); m_db.setDatabaseName(path); if(!m_db.open()) qDebug() << "Error: connection with database failed"; else qDebug() << "Database: connection ok"; qDebug() << m_db.lastError().text(); }
destructor:
DB::~DB() { m_db.close(); m_db.removeDatabase("mypath"); //value of "path" variable in constructor }
and i got:
Database: connection ok "" QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed. Database: connection ok "" "" QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work. QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed. Database: connection ok "" QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed. Database: connection ok "" QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed. Database: connection ok "" ""
Every time new db oject instantiated with
DB db = new DB();
i got these errors. Im usingdelete db
like you recommended.@masa4 There is really no need to add same database connection again and again. Do it once and use it. That is why I pointed you to the documentation. You can get existing database at any time in any place of your application using https://doc.qt.io/qt-6/qsqldatabase.html#database
To analyse the errors you posted: how many DB instances do you have at the same time? Because each of them will try to add same database...
-
@Emre-MUTLU
constructor:DB::DB(const QString &path) { m_db = QSqlDatabase::addDatabase("QSQLITE"); m_db.setDatabaseName(path); if(!m_db.open()) qDebug() << "Error: connection with database failed"; else qDebug() << "Database: connection ok"; qDebug() << m_db.lastError().text(); }
destructor:
DB::~DB() { m_db.close(); m_db.removeDatabase("mypath"); //value of "path" variable in constructor }
and i got:
Database: connection ok "" QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed. Database: connection ok "" "" QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work. QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed. Database: connection ok "" QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed. Database: connection ok "" QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed. Database: connection ok "" ""
Every time new db oject instantiated with
DB db = new DB();
i got these errors. Im usingdelete db
like you recommended.DB::~DB() { m_db.close(); m_db.removeDatabase(mypath); your path is string value already so you dont need to use("); }`
-
DB::~DB() { m_db.close(); m_db.removeDatabase(mypath); your path is string value already so you dont need to use("); }`
@Emre-MUTLU , @masa4
Up to you, but as @jsulm/documentation states you really should not have aQSqlDatabase m_db
member variable in a class. If you need to access theQSqlDatabase
instance use static QSqlDatabase QSqlDatabase::database(). -
@Emre-MUTLU , @masa4
Up to you, but as @jsulm/documentation states you really should not have aQSqlDatabase m_db
member variable in a class. If you need to access theQSqlDatabase
instance use static QSqlDatabase QSqlDatabase::database().DB::~DB() { m_db.close(); m_db=QSqlDatabase(); m_db.removeDatabase(mypath); your path is string value already so you dont need to use("); }
like that?
-
DB::~DB() { m_db.close(); m_db=QSqlDatabase(); m_db.removeDatabase(mypath); your path is string value already so you dont need to use("); }
like that?
@Emre-MUTLU
No! You have aQSqlDatabase m_db
member variable in yourDB
class. Start by getting rid of that variable and then make your code work! Remember that instead you can re-access the current database instance viaQSqlDatabase::database()
if you need it. You may haveQSqlDatabase db
variables local to methods if you wish, but no class member variable which persists for the lifetime of the class. -
@Emre-MUTLU
No! You have aQSqlDatabase m_db
member variable in yourDB
class. Start by getting rid of that variable and then make your code work! Remember that instead you can re-access the current database instance viaQSqlDatabase::database()
if you need it. You may haveQSqlDatabase db
variables local to methods if you wish, but no class member variable which persists for the lifetime of the class.@jsulm @JonB So you guys mean change code to like:
DB::DB(const QString &path) { QSqlDatabase::addDatabase("QSQLITE"); QSqlDatabase.setDatabaseName(path); //this doesnt work, the method is not static. if(!QSqlDatabase::open()) //non static, not work qDebug() << "Error: connection with database failed"; else qDebug() << "Database: connection ok"; qDebug() << QSqlDatabase::lastError().text(); //non static, not work }
I got errors from nonstatic functions in this way. And secondly if i try to call from another class( selectData() is method of DB class.)
otherclass.cpp://db = new DB(); //model->setQuery(db->selectData(ui->lineedit->text())); //normally its in this way model->setQuery(QSqlDatabase::database().selectData(ui->lineedit->text())); //No member named 'selectData' in 'QSqlDatabase'
How should i modify my class actually?
-
@jsulm @JonB So you guys mean change code to like:
DB::DB(const QString &path) { QSqlDatabase::addDatabase("QSQLITE"); QSqlDatabase.setDatabaseName(path); //this doesnt work, the method is not static. if(!QSqlDatabase::open()) //non static, not work qDebug() << "Error: connection with database failed"; else qDebug() << "Database: connection ok"; qDebug() << QSqlDatabase::lastError().text(); //non static, not work }
I got errors from nonstatic functions in this way. And secondly if i try to call from another class( selectData() is method of DB class.)
otherclass.cpp://db = new DB(); //model->setQuery(db->selectData(ui->lineedit->text())); //normally its in this way model->setQuery(QSqlDatabase::database().selectData(ui->lineedit->text())); //No member named 'selectData' in 'QSqlDatabase'
How should i modify my class actually?
@masa4 said in Returning QSqlQueryModel for TableView:
So you guys mean change code to like
No, we only suggested to not to have the m_db member variable in your DB class. I don't know why you changed model->setQuery part.
-
@masa4 said in Returning QSqlQueryModel for TableView:
So you guys mean change code to like
No, we only suggested to not to have the m_db member variable in your DB class. I don't know why you changed model->setQuery part.
@jsulm Ho okay i see, so I will continue to use
db = new DB(); some code delete DB();
paradigm, right? And what can u say for other non static methods? Without
m_db
how will i use them? In destructor i cant useQSqlDatabase::close()
without member variable too. -
DB::~DB() { m_db.close(); m_db.removeDatabase(mypath); your path is string value already so you dont need to use("); }`
@Emre-MUTLU No no i mean i used same path in both sides.
constructor:m_db.setDatabaseName(DB_PATH);
destructor
m_db.removeDatabase(DB_PATH);
I am passing same value to both but i keep getting this error every time
new DB();
called i think:QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
-
@jsulm Ho okay i see, so I will continue to use
db = new DB(); some code delete DB();
paradigm, right? And what can u say for other non static methods? Without
m_db
how will i use them? In destructor i cant useQSqlDatabase::close()
without member variable too.@masa4 said in Returning QSqlQueryModel for TableView:
And what can u say for other non static methods? Without m_db how will i use them?
Which non static members do you mean? As already explained you can get the database instance you need by simply calling QSqlDatabase::database().
And with your current implementation you're still creating the connection everytime you create a DB instance. You should use https://doc.qt.io/qt-6/qsqldatabase.html#contains to check whether the connection already exist and only add the connection if it does not yet exist.
-
@Emre-MUTLU No no i mean i used same path in both sides.
constructor:m_db.setDatabaseName(DB_PATH);
destructor
m_db.removeDatabase(DB_PATH);
I am passing same value to both but i keep getting this error every time
new DB();
called i think:QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
@masa4
i think you db name defaultly is qt_sql_default_connectionQSqlDatabase::removeDatabase("qt_sql_default_connection");
try this
-
@masa4 said in Returning QSqlQueryModel for TableView:
And what can u say for other non static methods? Without m_db how will i use them?
Which non static members do you mean? As already explained you can get the database instance you need by simply calling QSqlDatabase::database().
And with your current implementation you're still creating the connection everytime you create a DB instance. You should use https://doc.qt.io/qt-6/qsqldatabase.html#contains to check whether the connection already exist and only add the connection if it does not yet exist.
@jsulm You mean like this? :
DB::DB(const QString &path) { if(!QSqlDatabase::contains(QSqlDatabase::database().connectionName())){ QSqlDatabase::addDatabase("QSQLITE"); QSqlDatabase::database().setDatabaseName(path); } if(!QSqlDatabase::database().open()) qDebug() << "Error: connection with database failed"; else qDebug() << "Database: connection ok"; qDebug() << QSqlDatabase::database().lastError().text(); } DB::~DB() { QSqlDatabase::database().close(); }
-
@masa4
i think you db name defaultly is qt_sql_default_connectionQSqlDatabase::removeDatabase("qt_sql_default_connection");
try this
@Emre-MUTLU Yes when i add this i dont see the error output, but i changed the code again.
-
@jsulm You mean like this? :
DB::DB(const QString &path) { if(!QSqlDatabase::contains(QSqlDatabase::database().connectionName())){ QSqlDatabase::addDatabase("QSQLITE"); QSqlDatabase::database().setDatabaseName(path); } if(!QSqlDatabase::database().open()) qDebug() << "Error: connection with database failed"; else qDebug() << "Database: connection ok"; qDebug() << QSqlDatabase::database().lastError().text(); } DB::~DB() { QSqlDatabase::database().close(); }
@masa4
If it helps, you are welcome to use a local variable in a method to reference a database, just that you should not keep it around in a class member variable. You might have code looking like this (only a suggestion):DB::DB(const QString &path) { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(path); if (!db.open()) qDebug() << db.lastError().text(); } DB::~DB() { bool valid = false; QString connectionName = ""; { // the need for these enclosing parentheses for scope is explained at https://doc.qt.io/qt-6/qsqldatabase.html#removeDatabase // only because I am going to call `removeDatabase()` here QSqlDatabase db = QSqlDatabase::database(); valid = db.isValid(); if (valid) { connectionName = db.connectionName(); if (db.isOpen()) db.close(); } } if (valid && !connectionName.isEmpty()) QSqlDatabase::removeDatabase(connectionName); }