官网下载号对应的驱动并安装
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("127.0.0.1");
db.setPort(3306);
db.setDatabaseName("刚刚在ODBC中创建的Data source name"); //不是Database,记得别填错
db.setUserName("用户名");
db.setPassword("密码");
bool ok = db.open();
if (ok){
QMessageBox::information(this, "infor", "link success");
}
else {
QMessageBox::information(this, "infor", "link failed");
qDebug()<<"error open database because"<<db.lastError().text();
}
我是参照这个大神连接的: