gpt4 book ai didi

python - 如何通过odbc用python连接mysql

转载 作者:行者123 更新时间:2023-11-29 04:58:00 24 4
gpt4 key购买 nike

我在 Fedora 14 x64 上运行 mysql、pyodbc、python 2.7。

Odbcinst.ini 是:

# Example driver definitions

# Driver from the postgresql-odbc package
# Setup from the unixODBC package
#[PostgreSQL]
#Description = ODBC for PostgreSQL
#Driver = /usr/lib/psqlodbc.so
#Setup = /usr/lib/libodbcpsqlS.so
#Driver64 = /usr/lib64/psqlodbc.so
#Setup64 = /usr/lib64/libodbcpsqlS.so
#FileUsage = 1


# Driver from the mysql-connector-odbc package
# Setup from the unixODBC package
[MySQL]
Description = ODBC for MySQL
#Driver = /usr/lib/libmyodbc5.so
#Setup = /usr/lib/libodbcmyS.so
Driver64 = /usr/lib64/libmyodbc5.so
Setup64 = /usr/lib64/libodbcmyS.so
FileUsage = 1

Odbc.ini 是:

[MySQL]
Driver = MySQL
Database = mysql
Server = localhost
Socket = /var/lib/mysql/mysql.sock
User = rooter
Password = sshh

Mysql.sock 是空的?/var/lib/mysql/mysql.sock 有 0.B

python 脚本是:

import pyodbc

#pyodbc.pooling = False
conn = pyodbc.connect('DRIVER={MySQL};SOCKET=/var/lib/mysql/mysql.sock;UID=rooter;PWD=sshh')

csr = conn.cursor()
csr.execute("SET GLOBAL event_scheduler = ON")
csr.close()

conn.close()
del csr

我似乎无法连接上面的脚本,使用 isql 我可以连接!

我的错误消息:

Traceback (most recent call last):
File "/CCX/Py/MySql Event OFF.py", line 4, in <module>
conn = pyodbc.connect('DRIVER={MySQL};SOCKET=/var/lib/mysql/mysql.sock;UID=rooter;PWD=sshh')
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnectW)')

在 Win XPx64 上,

#cxn = MySQLdb.connect (host = "localhost",user="rooter",passwd ="sshh")

错误信息:

File "C:\Python26\lib\site-packages\MySQLdb\__init__.py", line 19, in <module>
import _mysql
ImportError: DLL load failed: %1 is not a valid Win32 application.

最佳答案

比较 odbcinst.ini 和你的 odbc.ini 显示 odbc.ini 中驱动程序的奇数值,你应该有一个动态库在这里(我想)。

你为什么不use direct mysql connection (没有 odbc)? :

import _mysql

conn = _mysql.connect(host="localhost", user="rooter",
password="sshh", db="mysql")
conn.query("SET GLOBAL event_scheduler = ON")
conn.close()

(未测试)

关于python - 如何通过odbc用python连接mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4665342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com