pyodbc and mySQL

佐手、 提交于 2019-11-30 20:54:43

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.

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.

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)

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`

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!