Search code examples
mysqlqtqtsql

Qt. Is connection name valuable to connect to the MySQL server?


Is connection name valuable to connect to the MySQL server?

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "conectionName");

Solution

  • No it is not, If you don't provide then a default name will be given but you can use that name to get that connection some where else.

    QSqlDatabase db = QSqlDatabase::database("connectionName");
    

    Here's what documentation says.

    If connectionName is not specified, the new connection becomes the default connection for the application, and subsequent calls to database() without the connection name argument will return the default connection. If a connectionName is provided here, use database(connectionName) to retrieve the connection.

    So if you don't provide any name, then whenever the below will return you that connection.

    QSqlDatabase db = QSqlDatabase::database();