我在使用pyodbc对本地mysql数据库时遇到了一些麻烦。这是一些版本信息:
➜ ~ python3 --version
Python 3.6.2
➜ ~ pip3 show pyodbc
Name: pyodbc
Version: 4.0.21
Summary: DB API Module for ODBC
Home-page: https://github.com/mkleehammer/pyodbc
Author: Michael Kleehammer
Author-email: michael@kleehammer.com
License: MIT
Location: /usr/local/lib/python3.6/site-packages
Requires:
➜ ~ odbcinst -j
unixODBC 2.3.4
DRIVERS............: /usr/local/Cellar/unixodbc/2.3.4/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/Cellar/unixodbc/2.3.4/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/Cellar/unixodbc/2.3.4/etc/ODBCDataSources
USER DATA SOURCES..: /Users/cognite/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
➜ ~ cat /usr/local/Cellar/unixodbc/2.3.4/etc/odbcinst.ini
[ODBC Drivers]
MySQL ODBC 5.3 Unicode Driver = Installed
MySQL ODBC 5.3 ANSI Driver = Installed
MySQL ODBC 5.3 Driver = Installed
[ODBC Connection Pooling]
PerfMon = 0
Retry Wait =
[MySQL ODBC 5.3 Unicode Driver]
Driver = /usr/local/mysql-connector-odbc-5.3.9-macos10.12-x86-64bit/lib/libmyodbc5w.so
[MySQL ODBC 5.3 ANSI Driver]
Driver = /usr/local/mysql-connector-odbc-5.3.9-macos10.12-x86-64bit/lib/libmyodbc5a.so
[MySQL ODBC 5.3 Driver]
Driver = /usr/local/lib/libmyodbc5w.so
➜ ~ cat /usr/local/Cellar/unixodbc/2.3.4/etc/odbc.ini
[ODBC Data Sources]
myodbc = MySQL ODBC 5.3 Unicode Driver
myodbca = MySQL ODBC 5.3 ANSI Driver
[ODBC]
Trace = 0
TraceAutoStop = 0
TraceFile =
TraceLibrary =
[myodbc]
Driver = MySQL ODBC 5.3 Unicode Driver
SERVER = localhost
PORT = 3306
[myodbca]
Driver = MySQL ODBC 5.3 ANSI Driver
SERVER = localhost
PORT = 3306
当我登录mysql(使用test
然后输入密码)时,我可以清楚地看到mysql -u root -p
数据库。 show databases
给出:
...
| mysql |
| performance_schema |
| sys |
| test |
+------------------------------------------------------+
189 rows in set (0.00 sec)
所以我不确定为什么我与pyodbc的连接尝试失败了。这是我尝试连接的python代码和我看到的输出:
import pyodbc
pyodbc.drivers()
# ['ODBC Drivers', 'ODBC Connection Pooling', 'MySQL ODBC 5.3 Unicode Driver', 'MySQL ODBC 5.3 ANSI Driver', 'MySQL ODBC 5.3 Driver']
CONNECTION_STRING = "driver={MySQL ODBC 5.3 Unicode Driver};SERVER=localhost;DATABASE=test;UID=root;PWD=root;CHARSET=UTF8"
conn = pyodbc.connect(CONNECTION_STRING)
# Traceback (most recent call last):
# File "<input>", line 1, in <module>
# pyodbc.Error: ('H000', '[H000] [unixODBC][ (0) (SQLDriverConnect)')
错误是什么意思?任何有关如何建立与本地数据库连接的帮助都将不胜感激。
更新:我必须使用Ansi驱动程序,然后才能运行。但我的问题仍然存在:该错误信息是什么意思?如何获取有关错误的更多信息?我本来希望它能说出“#34;无法用这个驱动程序连接数据库&#34;