What is the cause of the error?

no matching function for call to 'QSqlDatabase::database(const char [5], QString&)' QSqlDatabase db = QSqlDatabase::database("dbsql", connectionName); bool createConnectionToThread(QString dbName) { QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", dbName); db.setDatabaseName("dgis"); db.setUserName("root"); db.setHostName("localhost"); db.setPassword("golos123"); qDebug() << db.drivers(); if (!db.open()) { qDebug() << "Cannot open database: " << db.lastError(); return false; } return true; } ... QString connectionName = QString(random.go()); createConnectionToThread(connectionName); QSqlDatabase db = QSqlDatabase::database("dgis", connectionName); ... 
  • open the documentation and look at the database function - there is such a signature QSqlDatabase QSqlDatabase::database(const QString &connectionName = QLatin1String( defaultConnection ), bool open = true) - that is, two parameters - string and boolean. And you are trying to give him a string and a string. - KoVadim
  • @KoVadim you are absolutely right, please issue in the form of an answer, I will credit it. - shaman888

1 answer 1

According to the documentation , the database function signature is QSqlDatabase QSqlDatabase::database(const QString &connectionName = QLatin1String( defaultConnection ), bool open = true) - that is, two parameters — string and boolean. And you are trying to give him a string. Naturally it can not. And what is there to enter - it is necessary to understand, most likely true.