如何通过odbc与python连接到mysql

时间:2011-01-12 04:29:01

标签: python mysql pyodbc unixodbc

我在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我得到了连接!

MyERROR msg:

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")

Err Msg:

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.

3 个答案:

答案 0 :(得分:2)

比较odbcinst.ini和您的odbc.iniodbc.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() 

(未经测试)

答案 1 :(得分:1)

文件&#34; C:\ Python26 \ lib \ site-packages \ MySQLdb__init __。py&#34;,第19行,in     import _mysql ImportError:DLL加载失败:%1不是有效的Win32应用程序。

如果Python26&amp;&amp;视窗    我们需要下载3个文件:libguide40.dll,libmmd.dll,libmySQL.dll    将3个文件放到C:\ Python26 \ Lib \ site-packages中    并将3个文件放到C:\ Windows \ system32    然后重启你的系统

如果Python27&amp;&amp;视窗    设置模块时,请注意版本    MySQL的 - 蟒蛇-1.2.3.win32-py2.7.exe    MySQL的 - 蟒蛇-1.2.4.win32-py2.7.exe    你需要的2个版本之一

答案 2 :(得分:1)

来源:http://mysql-python.sourceforge.net/MySQLdb.html#some-mysql-examples

代码测试:

import _mysql

conn=_mysql.connect(host="localhost", user="root", passwd="***", db="test")
conn.query("select * from test_table")
result = conn.use_result()

print result.fetch_row()

fetch_row()函数有两个参数:

  1. maxrows限制返回的最大行数。
  2. how:当how = 0时返回一个元组,并在how = 1时返回一个字典。
  3. 例如,result.fetch(maxrows=5, how=1)返回一个字典数组(最大值:5),其中键是列名,值是列值。