使用 Excel VBA、ODBC 连接查询 MySQL 表 [英] Query MySQL Table with Excel VBA, ODBC Connection

查看:110
本文介绍了使用 Excel VBA、ODBC 连接查询 MySQL 表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试从 MySQL 8.0 表中获取值时遇到了 Microsoft Excel 的一些问题.我有一些从网上获得的代码(如下),但我不断收到错误消息.
我对此很陌生,所以需要一些帮助.

I've got a bit of an issue with Microsoft Excel while trying to get values from an MySQL 8.0 Table. I have some code (below) I got from the net but I keep getting an error message.
I'm quite new at this so I need a bit of help.

我使用的是 64 位计算机的 Windows 10.
时间表:
→ 我一周前下载了 MySQL,获得了 8.0 工作台版本并安装了连接器 (Connector/ODBC 8.0.12 - X64).
→ 我在 MySQL Workbench
中创建了一个名为 sap 的架构→ 我在 sap 中创建了一个名为 variance 的表格,其中填充了数据
到现在为止还挺好.
然后,我加载 Excel,获取Microsoft ActiveX 数据对象 6.1 库"和Microsoft Forms 2.0 对象库",创建一个模块并插入以下代码:

I'm on Windows 10 with a 64 bit computer.
Timeline:
→ I downloaded MySQL a week ago, got the 8.0 workbench version and have the connector installed (Connector/ODBC 8.0.12 - X64).
→ I created a Schema called sap in the MySQL Workbench
→ I created a Table in sap called variance filled with data
So far, so good.
Then, I load up Excel, get the 'Microsoft ActiveX Data Objects 6.1 Library' and 'Microsoft Forms 2.0 Object Library' create a Module and insert the following code :

Sub MySQL()

Dim conn As New ADODB.Connection
Dim SQL As String
Dim rs As ADODB.Recordset

Set conn = New ADODB.Connection

conn.Open "DRIVER={MySQL ODBC 8.0 Driver}" _
& ";SERVER=" & "localhost" _
& ";DATABASE=" & "sap" _
& ";USER=" & "root" _
& ";PASSWORD=" & "password" _
& ";OPTION=3"

Set rs = New ADODB.Recordset

SQL = "SELECT * FROM sap.variance;"
rs.Open SQL, conn

ThisWorkbook.Sheets(1).Range("A1").CopyFromRecordset rs

rs.Close
Set rs = Nothing

End Sub

文件在我的电脑上,所以本地主机对我有用,我也试过127.0.0.1"
数据库(MySQL 的架构)称为 sap
我使用的用户是 root 用户
密码就是密码(不是真的,但我不是说真的)

The file is on my computer so localhost works for me, I also tried with "127.0.0.1"
The database (schema for MySQL) is called sap
The user I'm using is the root user
And password is password (not really but I'm not telling for real)

应该可以,不是吗?然后,致命的部分:

Should work, shouldn't it ? And then, the fatal part :

[Microsoft][ODBC 驱动程序管理器] 未找到数据源名称且未指定设备驱动程序"

"[Microsoft][ODBC Driver Manager] Data Source name not found and no device driver specified"

调试突出显示行'conn.Open ....

Debug highlights the line 'conn.Open ....

我做错了什么?

推荐答案

如果你的 Excel 是 64 位的,你需要 MySQL odbc 64 位.如果您的 Excel 是 32 位,则您需要 ODBC 32 位(即使您的 Windows 是 64 位).

If your Excel is 64 bits, you need MySQL odbc 64 bits. If your Excel is 32 bits, you need ODBC 32 bits (even your Windows is 64 bits).

了解您的 Excel 是 32 位还是 64 位的简单方法,打开 Excel 和任务管理器.在进程选项卡中,查看 Excel 是否有(32 位)后缀.

An easy way to know if your Excel is 32 or 64 bits, open Excel and Task Manager. In processus tab, see if Excel has (32 bit) suffix.

这篇关于使用 Excel VBA、ODBC 连接查询 MySQL 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆