@@ -1277,6 +1277,15 @@ bool QMYSQLDriver::open(const QString& db,
12771277 unixSocket.isNull () ? nullptr : unixSocket.toUtf8 ().constData (),
12781278 optionFlags);
12791279
1280+ if (mysql != d->mysql ) {
1281+ setLastError (qMakeError (tr (" Unable to connect" ),
1282+ QSqlError::ConnectionError, d));
1283+ mysql_close (d->mysql );
1284+ d->mysql = nullptr ;
1285+ setOpenError (true );
1286+ return false ;
1287+ }
1288+
12801289 // now ask the server to match the charset we selected
12811290 if (!cs || mysql_set_character_set (d->mysql , cs->csname ) != 0 ) {
12821291 bool ok = false ;
@@ -1292,30 +1301,15 @@ bool QMYSQLDriver::open(const QString& db,
12921301 mysql_character_set_name (d->mysql ));
12931302 }
12941303
1295- if (mysql == d->mysql ) {
1296- if (!db.isEmpty () && mysql_select_db (d->mysql , db.toUtf8 ().constData ())) {
1297- setLastError (qMakeError (tr (" Unable to open database '%1'" ).arg (db), QSqlError::ConnectionError, d));
1298- mysql_close (d->mysql );
1299- setOpenError (true );
1300- return false ;
1301- }
1302- if (reconnect)
1303- mysql_options (d->mysql , MYSQL_OPT_RECONNECT, &reconnect);
1304- } else {
1305- setLastError (qMakeError (tr (" Unable to connect" ),
1306- QSqlError::ConnectionError, d));
1304+ if (!db.isEmpty () && mysql_select_db (d->mysql , db.toUtf8 ().constData ())) {
1305+ setLastError (qMakeError (tr (" Unable to open database '%1'" ).arg (db), QSqlError::ConnectionError, d));
13071306 mysql_close (d->mysql );
1308- d->mysql = nullptr ;
13091307 setOpenError (true );
13101308 return false ;
13111309 }
13121310
1313- // force the communication to be utf8mb4 (only utf8mb4 supports 4-byte characters)
1314- if (mysql_set_character_set (d->mysql , " utf8mb4" )) {
1315- // this failed, try forcing it to utf (BMP only)
1316- if (mysql_set_character_set (d->mysql , " utf8" ))
1317- qWarning () << " MySQL: Unable to set the client character set to utf8." ;
1318- }
1311+ if (reconnect)
1312+ mysql_options (d->mysql , MYSQL_OPT_RECONNECT, &reconnect);
13191313
13201314 d->preparedQuerysEnabled = checkPreparedQueries (d->mysql );
13211315
0 commit comments