如何使用python通过odbc连接mysql

发布于 2024-10-11 13:02:07 字数 1992 浏览 4 评论 0原文

我正在运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

<逆流佳人身旁 2024-10-18 13:02:07

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

为什么不使用直接mysql连接(没有odbc)? :(

import _mysql

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

未测试)

Comparing odbcinst.ini and your odbc.ini show an odd value for driver in odbc.ini, you should have a dynamic library here (I think).

And why don't you use direct mysql connection (without odbc)? :

import _mysql

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

(not tested)

不甘平庸 2024-10-18 13:02:07

文件“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

最丧也最甜 2024-10-18 13:02:07

来源: 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时返回一个字典。

例如,result.fetch(maxrows=5, how=1) 返回一个字典数组(最大大小:5),其中键是列名称,值是列值。

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

code tested:

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

The fetch_row() function has two parameters:

  1. maxrows limits the max number of rows returned.
  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文