如何使用python通过odbc连接mysql
我正在运行 mysql、pyodbc、在 Fedora 14 x64 上加载的 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 消息:
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.
Im running mysql, pyodbc, python 2.7 loaded on Fedora 14 x64.
Odbcinst.ini is:
# 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 is :
[MySQL]
Driver = MySQL
Database = mysql
Server = localhost
Socket = /var/lib/mysql/mysql.sock
User = rooter
Password = sshh
Mysql.sock is empty?
/var/lib/mysql/mysql.sock has 0.B
python script is:
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
I cant seem to connect with above script, Using isql i get Connected!
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)')
On 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
比较
odbcinst.ini
和您的odbc.ini
在odbc.ini
中显示驱动程序的奇怪值,您应该在这里有一个动态库(我思考)。为什么不使用直接mysql连接(没有odbc)? :(
未测试)
Comparing
odbcinst.ini
and yourodbc.ini
show an odd value for driver inodbc.ini
, you should have a dynamic library here (I think).And why don't you use direct mysql connection (without odbc)? :
(not tested)
文件“C:\Python26\lib\site-packages\MySQLdb__init__.py”,第 19 行,位于
导入_mysql
导入错误:DLL 加载失败:%1 不是有效的 Win32 应用程序。
如果Python26 &&视窗
我们需要下载3个文件:libguide40.dll、libmmd.dll、libmySQL.dll
将这3个文件放入C:\Python26\Lib\site-packages
并将这3个文件放入C:\Windows\system32
则重新启动系统
如果 Python27 && 视窗
设置模块时,请注意版本
MySQL-python-1.2.3.win32-py2.7.exe
MySQL-python-1.2.4.win32-py2.7.exe
您需要 2 个版本之一
File "C:\Python26\lib\site-packages\MySQLdb__init__.py", line 19, in
import _mysql
ImportError: DLL load failed: %1 is not a valid Win32 application.
if Python26 && windows
we need download 3 files : libguide40.dll, libmmd.dll, libmySQL.dll
put the 3 files to C:\Python26\Lib\site-packages
and also put the 3 files to C:\Windows\system32
then restart your system
if Python27 && windows
when you setup the module ,be careful the version
MySQL-python-1.2.3.win32-py2.7.exe
MySQL-python-1.2.4.win32-py2.7.exe
one of the 2 version is you need
来源: http://mysql-python.sourceforge.net/MySQLdb.html #some-mysql-examples
测试代码:
fetch_row()
函数有两个参数:maxrows
限制返回的最大行数。how
:当how=0时返回一个元组,当how=1时返回一个字典。例如,
result.fetch(maxrows=5, how=1)
返回一个字典数组(最大大小:5),其中键是列名称,值是列值。source: http://mysql-python.sourceforge.net/MySQLdb.html#some-mysql-examples
code tested:
The
fetch_row()
function has two parameters:maxrows
limits the max number of rows returned.how
: it returns a tuple when how=0 and returns a dictionary when how=1.For example,
result.fetch(maxrows=5, how=1)
returns an array (maximum size: 5) of dictionaries where the key is the column name and the value is the column value.