Bobspadger / pyodbc

Automatically exported from code.google.com/p/pyodbc
MIT No Attribution
0 stars 0 forks source link

Unicode paths gives UnicodeEncodeError #108

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

Run a program similar to this:

  # encoding: utf-8
  import pyodbc
  MDB_CONN_STRING = u'DRIVER={{Microsoft Access Driver (*.mdb)}};DBQ={0};'
  def test(path):
      cnxn = pyodbc.connect(MDB_CONN_STRING.format(path))
      cursor = cnxn.cursor()
      cursor.execute('select * from DB')
      for row in cursor:
          print '.'
  test(ur'C:\path\without\unicode\chars.mdb')
  test(ur'C:\path\with\unicode\chars\åøæ.mdb')

What is the expected output? What do you see instead?

An UnicodeEncodeError is throw in the second invocation of test in the for 
loop. If you comment out the loop no exception is thrown.

What version of the product are you using? On what operating system?

pyodbc 2.1.7 on Windows XP with Python 2.6.2.

Original issue reported on code.google.com by mbu...@gmail.com on 12 Jul 2010 at 12:09

GoogleCodeExporter commented 9 years ago
Could you attach a test script?  I've pasted your example into emacs and 
notepad and I get a Python syntax error, so clearly I'm doing something wrong.

I've tried Python 2.6 and 2.7, 32 and 64-bit.

Thanks...

Original comment by mkleehammer on 26 Aug 2010 at 10:23

GoogleCodeExporter commented 9 years ago

Original comment by mkleehammer on 21 Nov 2010 at 6:07

GoogleCodeExporter commented 9 years ago
Hi

I had the same issue if need a unicode string to connect:
Systems tested: Windows 7, windows XP and Windows2000 sp4
Python version: 2.7
pyodbc version: 2.1.8

1. Created two copies of 1 database. 
   The first uses a pure ascii password
   the second uses a passwor wich contains a unicode character "yyy\xe9yyyy"
2. Create ODBC datasources for both databases an test they works properly
3. Retrive the DSN info from the system registry using a python script.
4. link the DSN info creating proper DSN strings DSN1 and DSN2
   the only difference is DSN2 contains the character \xe9 (in the registered password)
DSN1 = "DSN=Db1CopyDSNName;PWD=Db1password"
DSN2 = u"DSN=Db2CopyDSNName;PWD=Db2password"  #so it contains 1 unicode char.
5. create connections:
>>> cnn1 = pyodbc.connect(DSN1)   #SUCCESS
>>> cnn2 = pyodbc.connect(DSN2)   #SYSTEM EXCEPTION

6. The exception as was printed by IDLE:
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    cnn = pyodbc.connect('DSN=adsntodb1PwdUtf;PWD=Némesis')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 25: 
ordinal not in range(128)

SUGESTION: I've got the same exception when reading from databases wich 
contains utf strings and manipulate the outputs. I've solved it creating a 
simple script to ensure all the strings, I use, are unicode. I supouse the 
codec exception raises when pyodbc links the dsn to perform operations (perhaps 
before passig the info to your C libraries)

Original comment by luisguil...@gmail.com on 18 Jul 2011 at 7:04