[英]How connect to mysql with python via odbc
Im running mysql, pyodbc, python 2.7 loaded on Fedora 14 x64. 我在Fedora 14 x64上运行mysql,pyodbc,python 2.7。
Odbcinst.ini is: 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 is : Odbc.ini是:
[MySQL]
Driver = MySQL
Database = mysql
Server = localhost
Socket = /var/lib/mysql/mysql.sock
User = rooter
Password = sshh
Mysql.sock is empty? Mysql.sock是空的? /var/lib/mysql/mysql.sock has 0.B
/var/lib/mysql/mysql.sock有0.B
python script is: 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
I cant seem to connect with above script, Using isql i get Connected! 我似乎无法连接上面的脚本,使用isql我得到连接!
MyERROR msg: 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)')
On Win XPx64, 在Win XPx64上,
#cxn = MySQLdb.connect (host = "localhost",user="rooter",passwd ="sshh")
Err Msg: 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.
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). 比较
odbcinst.ini
和你odbc.ini
显示在驾驶奇数值odbc.ini
,你应该有一个动态库在这里(我认为)。
And why don't you use direct mysql connection (without odbc)? 为什么不使用直接的mysql连接 (没有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) (未测试)
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. 导入_mysql文件“C:\\ Python26 \\ lib \\ site-packages \\ MySQLdb__init __。py”,第19行ImportError:DLL加载失败:%1不是有效的Win32应用程序。
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 如果Python26 && windows我们需要下载3个文件:libguide40.dll,libmmd.dll,libmySQL.dll将3个文件放到C:\\ Python26 \\ Lib \\ site-packages中,并将3个文件放到C:\\ Windows \\ system32然后重启你的系统
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 如果在设置模块时使用Python27 && windows,请注意版本MySQL-python-1.2.3.win32-py2.7.exe MySQL-python-1.2.4.win32-py2.7.exe其中一个版本是你需要
source: http://mysql-python.sourceforge.net/MySQLdb.html#some-mysql-examples 来源: 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: fetch_row()
函数有两个参数:
maxrows
limits the max number of rows returned. maxrows
限制返回的最大行数。 how
: it returns a tuple when how=0 and returns a dictionary when how=1. how
:当how = 0时返回一个元组,当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. 例如,
result.fetch(maxrows=5, how=1)
返回字典的数组(最大值:5),其中键是列名,值是列值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.