QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"/*"QODBC"*/); // becomes the new default connection db.setUserName("root");//用户名 db.setPassword("password");//密码 db.setHostName("localhost"); db.setDatabaseName("test");//数据库名 db.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1");//使用SSL db.setPort(3306);//端口 if(db.open()) { qDebug() << "open\n" << db.lastError().driverText() << "\n"; } else { qDebug() << "open faile\n"; }