简体   繁体   中英

How to connect to access (.mdb) database with pyodbc using latin-1 filename

I use this code to connect to my access (.mdb) database:

# -*- coding: latin-1 -*-
fileMDB = 'C:\\Python27\\OptimisateurLievre\\final\\Archives_PrevisionsESP_Août_2013.mdb'
param = "Driver={Microsoft Access Driver (*.mdb)};DBQ={%s};PWD={pw}" % fileMDB
con = odbc.connect(param)

I get the following error:

pyodbc.Error: ('HY000', '[HY000] [Microsoft][Pilote ODBC Microsoft Access] Filename incorrect. (-1044) (SQLDriverConnect); [HY000] [Microsoft][Pilote ODBC Microsoft Access] Filename incorrect. (-1044)')

The problem seems to come from the database filename with the û caracter. To my understanding of string and unicode, fileMDB is a string encoded in latin-1. Since, my computer runs with latin-1 encoding I don't understand why the filename is incorrect.

I work with Windows XP and python 2.7.

Thank you for your help!

It appears that pyodbc tries to convert the connection string to 'ascii' , so any characters above 0x7F are invalid:

 con = pyodbc.connect(param) 

UnicodeDecodeError: 'ascii' codec can't decode byte 0xfb in position 84: ordinal not in range(128)

However, I was able to get it to work using pypyodbc :

# -*- coding: cp1252 -*-
import pypyodbc
fileMDB = r'C:\__tmp\test\Archives_PrevisionsESP_Août_2013.mdb'
param = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" + fileMDB
con = pypyodbc.connect(param)
print 'Connection established.'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM