QT5.15.1连接MySQL8.0.28(32位/64位)最佳实践(ODBC)
一、如果需要32位
下载ODBC-mysql驱动:(mysql-connector-odbc-8.0.28-win32.msi)
MySQL :: Download Connector/ODBC
安装后可以在“控制面板\所有控制面板项\管理工具\ODBC 数据源(32 位)”看到数据源
代码如下:
qDebug() << QSqlDatabase::drivers();
//if(::SQLValidDSNW(L"testdb") == FALSE)
{
qDebug()<<"add DSN testdb\n";
// 自动注册MySQL ODBC数据源
::SQLConfigDataSourceW(NULL, ODBC_ADD_DSN, L"MySQL ODBC 8.0 Unicode Driver", L"DSN=testdb;DATABASE=testdb;SERVER=127.0.0.1;PORT=3306;USER=test;PASSWORD=123456;");
}
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("192.168.2.129");
db.setPort(3306);
db.setDatabaseName("testdb");
db.setUserName("test");
db.setPassword("123456");
bool ok = db.open();
if (ok){
QMessageBox::information(this, "infor", "success");
}
else {
QMessageBox::information(this, "infor", "open failed");
qDebug()<<"error open database because"<<db.lastError().text();
}
二、如果需要64位
下载ODBC-mysql驱动:(mysql-connector-odbc-8.0.28-winx64.msi)
MySQL :: Download Connector/ODBC
安装后可以在“控制面板\所有控制面板项\管理工具\ODBC 数据源(64 位)”看到数据源
qDebug() << QSqlDatabase::drivers();
//if(::SQLValidDSNW(L"testdb") == FALSE)
{
qDebug()<<"add DSN testdb\n";
// 自动注册MySQL ODBC数据源
::SQLConfigDataSourceW(NULL, ODBC_ADD_DSN, L"MySQL ODBC 8.0 Unicode Driver", L"DSN=testdb;DATABASE=testdb;SERVER=127.0.0.1;PORT=3306;USER=test;PASSWORD=123456;");
}
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("192.168.2.129");
db.setPort(3306);
db.setDatabaseName("testdb");
db.setUserName("test");
db.setPassword("123456");
bool ok = db.open();
if (ok){
QMessageBox::information(this, "infor", "success");
}
else {
QMessageBox::information(this, "infor", "open failed");
qDebug()<<"error open database because"<<db.lastError().text();
}
ODBC-Windows自动注册安装驱动
{
BOOL bRet = FALSE;
WORD wBufMax = 0xFFFF;
WORD wBufOut = 0xFFFF;
LPWSTR lpwBuf = decltype(lpwBuf)(malloc(wBufMax * sizeof(WCHAR)));
if (lpwBuf != NULL)
{
bRet = ::SQLGetInstalledDriversW(lpwBuf, wBufMax, &wBufOut);
LPWSTR lpw = lpwBuf;
for (decltype(wBufOut) i = 0; (i < wBufOut) && (lpw != NULL) && ((*lpw) != NULL); i++, lpw += lstrlenW(lpw) + 1)
{
printf("%ws\n", lpw);
OutputDebugStringW(lpw);
OutputDebugStringW(L"\n");
}
{
WORD wPathOut = 0;
DWORD dwUsageCount = 0;
LPWSTR lpwPathOut = NULL;
LPCWSTR lpcwTargetPath = L"C:\\Windows\\System32";
//LPCWSTR lpcwDriver = L"Text\0Driver=TEXT.DLL\0Setup=TXTSETUP.DLL\0FileUsage=1\0FileExtns=*.txt,*.csv\0\0";
LPCWSTR lpcwDriver = L"MySQL ODBC 8.0 Unicode Driver\0Driver=myodbc8w.dll\0Setup=myodbc8S.dll\0\0";
//LPCWSTR lpcwDriver = L"MySQL ODBC 8.0 Ansi Driver\0Driver=myodbc8a.dll\0Setup=myodbc8S.dll\0\0";
bRet = ::SQLInstallDriverExW(lpcwDriver, lpcwTargetPath, NULL, 0, &wPathOut, ODBC_INSTALL_INQUIRY, &dwUsageCount);
if (bRet == TRUE)
{
lpwPathOut = decltype(lpwPathOut)(malloc(wPathOut * sizeof(WCHAR)));
if (lpwPathOut != NULL)
{
bRet = ::SQLInstallDriverExW(lpcwDriver, lpcwTargetPath, lpwPathOut, wPathOut, &wPathOut, ODBC_INSTALL_COMPLETE, &dwUsageCount);
free(lpwPathOut);
lpwPathOut = NULL;
}
}
}
{
bRet = ::SQLConfigDataSourceW(NULL, ODBC_ADD_DSN, L"MySQL ODBC 8.0 Unicode Driver", L"DSN=testdb;DATABASE=testdb;SERVER=127.0.0.1;PORT=3306;USER=test;PASSWORD=123456;");
}
{
DWORD dwUsageCount = 0;
LPCWSTR lpcwDriver = L"MySQL ODBC 8.0 Unicode Driver\0Driver=myodbc8w.dll\0Setup=myodbc8S.dll\0\0";
bRet = ::SQLRemoveDriverW(lpcwDriver, TRUE, &dwUsageCount);
}
free(lpwBuf);
lpwBuf = NULL;
}
}