QSQlQuery::exec: database not open
-
the only difference between the 2 code examples you posted is, that in one you pass the database to the queryModel.
You said you tested it, and it did not work, what exactly is the result when you would change the function to this:
void MainWindow::on_serachBox_textEdited(const QString& arg1) { handler->open(); queryModel.setQuery("SELECT * FROM " + ui->comboBox->currentText() + " WHERE " + handler->getMainField(ui->comboBox->currentText()) + " LIKE \'%" + arg1 + "%\'"); qDebug() << queryModel.lastError(); qDebug() << queryModel.query().lastQuery(); handler->close(); ui->dataOutput->setModel(&queryModel); }
I haven't used the
QSqlQueryModel
class myself yet, but are you supposed to to piece the query together like this!? Seems wrong. -
@J.Hilk hello,
The result is that I get the same meessge, QSqlQuery::exec: database not open, regardless whether I write handler->ope (); or not.
The QTableView, which I use this model with, would stop displaying any table.Basically no difference.
-
@Omar-Alkersh Can you show the content of handler->open() ?
-
@Omar-Alkersh said in QSQlQuery::exec: database not open:
QSqlDatabase db;
If this is like this in the constructor then it is a local variable in the constructor and not related to the db pointer!
You also should check the return value of db->open () and print out the output of http://doc.qt.io/qt-4.8/qsqldatabase.html#lastError in case it returns false. Also you did not say how you set up your database. -
Thanks for the suggestion but this is a global variable, it is just initialized in the constructor.
And this is how I set up my db
DBHandler::DBHandler(MainWindow* mWindow) { // init vars path = databaseloc.currentPath() + "/database"; TABLE_MAIN = "main_table"; COLUMN_ID = "_id"; COLUMN_MAIN_NAME = "main_name"; // check if dir exits QFileInfo fileInfo(path); if (!fileInfo.exists()) { if (databaseloc.mkpath(path)) { mWindow->close(); } } // db name and loc path += "/" + dbName + ".db"; // add db db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(path); // open test, sets up query and creates mainTable if not exists if (db.open()) { query = QSqlQuery(db); query.exec("CREATE TABLE IF NOT EXISTS " + TABLE_MAIN + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_MAIN_NAME + " TEXT, mainField TEXT);"); } else { // stop programme from continueing QMessageBox::StandardButton reply; reply = QMessageBox::warning( mWindow, "Error", "Was not able to open database file.\n" + db.lastError().text()); if (reply == QMessageBox::Ok) { // exits with error exit(1); } } db.close(); }
-
Did you notice the close at the end of your constructor?
} db.close(); }
I guess there is your problem.
-
@SGaist Thank you good sir. It worked. I thought that I should always close the db to stop any memory leaks. I used to develop android and I used to always close it after me.
Solution was not to close the db. Obviously.
-
AFAIK, there's no known memory leaks in Qt's SQL drivers.
Don't get me wrong, there might be good reasons to only open a database connection when needed however this requires a bit more architecture to make it work correctly.