Pyodbc and mySQL

I cannot 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 in the pyodbc.connect () line:

pyodbc.Error: ('IM002', '[IM002] [Microsoft] [ODBC driver manager] Data source name was not found and the specified driver is not used by default (0) (SQLDriverConnectW)')

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

Right now I'm a little lazy (without exception handling) - is this normal Python script behavior without exception handling to fail?

[change]

mysqldb, pyodbc (MS Access). , , pyodbc, // ( Python) "". Windows mySQl Linux. Linux, terra firma.

"script". .py, python myscript.py . XP

+6
6

MySQLdb ( oursql) pyodbc (DB-API 2), ODBC, . MySQLdb ( oursql).

+2

. , DSN, OPTION = 3:

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

, , DSN.

, Python . , script, - .

+2

mysql-connector-odbc-3.51.28-win32.msi.

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

, MySQL 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`
+2

, , . :

Python 2.7 32 :  - pyobbc 32  - 32 . (Microsoft Access 32 )

, 64- . , 64 .

Oracle Microsoft Access, :

  • pyodbc (MS Access)
  • cx_Oracle ( Oracle SQLalchemy)
  • Oracle instantclient basic (Oracle. )
  • py2exe ( excecutable)
+2

?

Windows โ†’ โ†’ โ†’ โ†’ ODBC โ†’

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

, .

0

I get the same error. It seemed that the driver I used to connect was not installed on my system. Type ODBC into Windows run and select ODBC Data Source (32/64) depending on where you installed the driver. From there, click System DSN and click Add. From there, you can see the MySQL driver installed on your system. Use the ANSI driver in your code where you establish the connection.

0
source

All Articles