pyodbc和mySQL [英] pyodbc and mySQL

查看:557
本文介绍了pyodbc和mySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用pyodbc连接到mySQl数据库。

I am unable to connect to mySQl db using pyodbc.

这是一个脚本的代码片段:

Here is a snippet of my script:

import pyodbc
import csv

cnxn = pyodbc.connect("DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost;DATABASE=mydb; UID=root; PASSWORD=thatwouldbetelling;") 
crsr = cnxn.cursor()

with open('C:\\skunkworks\\archive\\data\\myfile.csv','r') as myfile:
    rows = csv.reader(myfile, delimiter=',', quotechar='"')
    for row in rows:
        insert_str = 'INSERT into raw_data VALUES(something, something)'
        print insert_str
        #crsr.execute(insert_str)
    cnxn.commit()
    myfile.close()

我在pyodbc.connect()行中收到此错误:

I get this error at the pyodbc.connect() line:


pyodbc.Error:('IM002','[IM002]
[Microsoft] [ODBC驱动程序管理器]数据
未找到源名称而没有默认
驱动程序指定(0)
(SQLDriverConnectW)')

pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnectW)')

我有另一个有关此错误的问题(和Python一般脚本)。当我作为一个脚本运行它,它会默默地失败(我期待一个堆栈跟踪)。我必须手动键入每一行以找到发生错误的位置。

I have another question regarding this error (and Python scripts in general). When I run this as a script, it fails silently (I was expecting a stack trace). I have to type each line in manually to find where the error occured.

我现在有点懒惰(没有异常处理) - 这是一个正常的行为Python脚本无异常处理可以静默失败?

I am being a bit lazy for now (no exception handling) - is this normal behaviour of a Python script without exception handling to fail silently?

我没有使用mysqldb,因为我已经使用pyodbc从另一个源(MS Access)提取我的数据。好的,不是一个很好的理由 - 但是我已经在琢磨pyodbc,而且我真的不喜欢与另一个库/模块/包(无论Python中调用)进行一次工作。我只想将我的Windows环境中的各种数据源的数据移动到Linux上的mySQl。一次在Linux上,我会回到terra firma。

I am not using mysqldb because I am already using pyodbc to extract my data from another source (MS Access). Ok, not a good reason - but I am already grappling with pyodbc and I dont really fancy having to wrestle with another library/module/package(whatever its called in Python) for a "one off" job. I just want to move my data of from various data sources in the Windows environment to mySQl on Linux. once on Linux, I'll be back on terra firma.

这就是整个脚本。我只是将代码保存在.py扩展名的文件中,我在命令行中运行python myscript.py。我正在XP机器上运行

That is the entire 'script' right there. I just saved the code above into a file with a .py extension, and I run python myscript.py at the command line. I am running this on my XP machine

推荐答案

只需要安装mysql-connector-odbc-3.51.28-win32.msi。

only need install mysql-connector-odbc-3.51.28-win32.msi.

和pyodbc-2.1.7.win32-py2.7.exe。

and pyodbc-2.1.7.win32-py2.7.exe.

当然,你有准备安装MySQL和python 2.7。

of course, you have ready installed MySQL and python 2.7.

示例:

import pyodbc

cndBase = pyodbc.connect("DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; PORT=3306;DATABASE=nameDBase; UID=root; PASSWORD=12345;") 
ptdBase = cndBase.cursor()

query_str = 'SELECT* FROM nameTabla;'
rows  = ptdBase.execute(query_str)

for rw in rows:
    print list(rw)`enter code here`

这篇关于pyodbc和mySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆