pyodbc mysql_pyodbc and mySQL
发布日期:2021-06-24 13:48:12 浏览次数:2 分类:技术文章

本文共 4476 字,大约阅读时间需要 14 分钟。

问题

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

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

pyodbc.Error: ('IM002', '[IM002]

[Microsoft][ODBC Driver Manager] Data

source name not found and no default

driver specified (0)

(SQLDriverConnectW)')

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.

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?

[Edit]

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.

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

回答1:

MySQLdb (or oursql) and pyodbc both have the same interface (DB-API 2), only you don't have to deal with ODBC's issues if you use the former. I strongly recommend you consider using MySQLdb (or oursql) instead.

回答2:

First, According to the official docs, if you want to connect without creating a DSN, you need to specify OPTION=3 in the connection string:

ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=test;USER=venu;PASSWORD=venu;OPTION=3;"

If that fails to work, I'd further troubleshoot by creating a DSN.

Second, no Python should not be failing silently. If that is the case when you run your script, there is something else amiss.

回答3:

I had this same mistake so I went over all the version I was using for the connection. This is what I found out:

For Python 2.7 32 bits:

- pyodbc must be 32bits

- the DB Driver must be 32bits. (Microsoft Access should be 32 bits too)

For those who use the 64 bits version. You should check that everything is 64 bits too.

In my case I was trying to connecto to an Oracle DB and Microsoft Access DB so I had to make the following components match the architechture version:

pyodbc (MS Access)

python

cx_Oracle (Oracle dialect for SQLalchemy)

Oracle instantclient basic (Oracle. Do not forget to create the environment variable)

py2exe (Making the excecutable app)

回答4:

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

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

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

example:

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`

回答5:

Is that your driver name right?

You can check your driver name in

Windows -> Control panel -> System and security -> Administrative tools -> ODBC Data Sources -> Driver tab

then copy the river name to the first parameter

like

cnxn = pyodbc.connect("DRIVER={MySQL ODBC 5.3 ANSI Driver}; SERVER=localhost;DATABASE=books; UID=root; PASSWORD=password;")

And my problem solved

or you may not install the driver and the step is simple.

回答6:

I was getting the same error. It seemed the driver i was using to make the connection was not the driver installed in my system.

Type ODBC on windows run and select ODBC Data Source(32/64) based on where you have installed the driver.

From there click on System DSN and click add. From there you can see the MySQL driver installed in your system. Use the ANSI driver in your code where you are making the connection.

来源:https://stackoverflow.com/questions/3982174/pyodbc-and-mysql

转载地址:https://blog.csdn.net/weixin_33245447/article/details/113303967 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:mysql5.0.77中文乱码_解决Mysql5.7中文乱码的问题
下一篇:mysql jsp简单增删改_学生信息管理系统--基于jsp技术和MySQL的简单增删改查

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月15日 08时41分11秒