2
votes

I am using

  • pyodbc 3.0.3
  • python 2.7.12
  • GNU/Linux 4.4.0-59-generic x86_64
  • Ubuntu 16.04.1 LTS (Xenial Xerus)
  • unixODBC 2.3.1
  • ODBC 5.3(w) Driver

When a try to pass a Unicode database name in the connection string I get the error

A Unicode connection string was supplied but the driver does not have a Unicode connect function

My Code looks like this

# -*- coding: utf-8 -*-

import pyodbc
dbname = u"डाटाबेस"
cstring = "DRIVER={MySQL};SERVER=192.168.8.25;PORT=3306;DATABASE="+dbname+";UID=root;PWD=root;CHARSET=utf8;" \
                                                                          "use_unicode=1"
connect = pyodbc.connect(cstring)

Edit 1:

I have updated the pyodbc to 4.0.21 version. The above error went aways but got another problem

import pyodbc
dbname = u"डाटाबेस"
cstring = "DRIVER={MySQL};SERVER=192.168.2.243;PORT=3306;DATABASE="+dbname+";UID=root;PWD=support@immune;CHARSET=utf8;" \
                                                                       "use_unicode=1"
connect = pyodbc.connect(cstring, encoding='utf-8')

When I run this I got following error

error=('HY000', u"[HY000] [unixODBC][MySQL][ODBC 5.3(w) Driver]Unknown database '\xe0\xa4\xa1\xe0\xa4\xbe\xe0\xa4\x9f\xe0\xa4\xbe\xe0\xa4\xac\xe0\xa5\x87\xe0\xa4\xb8' (1049) (SQLDriverConnect)")

1
Please edit your question to show: What OS you're running. What version of the MySQL ODBC driver you're using. How you installed that driver.Gord Thompson
pyodbc 3.0.3 is really old. Try pip install pyodbc==4.0.21 and see if that helps.Gord Thompson
Perhaps: Use MySQL Connector/Python instead of pyodbc and MySQL Connector/ODBCRick James
For now, changing library is not an option. Change in the library may lead to a lot of change in our product code.Sachin Aryal

1 Answers

1
votes

I was able to reproduce your issue and avoid it by removing the encoding='utf-8' parameter from the connect call. My test code

# -*- coding: utf-8 -*-
import pyodbc
import sys

print("Python version: " + sys.version.replace("\n", ""))
print("pyodbc version: " + pyodbc.version)
cnxn_str = (
    u"Driver=MySQL;"
    u"Server=192.168.1.144;"
    u"Port=3307;"
    u"Database=डाटाबेस;"
    u"UID=root;PWD=whatever;"
)
cnxn = pyodbc.connect(cnxn_str)
print("driver name: " + cnxn.getinfo(pyodbc.SQL_DRIVER_NAME))
print("driver version: " + cnxn.getinfo(pyodbc.SQL_DRIVER_VER))
crsr = cnxn.cursor()

print(crsr.execute("SELECT txt FROM table1 WHERE id = 1").fetchval())

cnxn.close()

produced

Python version: 2.7.12 (default, Dec  4 2017, 14:50:18) [GCC 5.4.0 20160609]
pyodbc version: 4.0.21
driver name: libmyodbc5w.so
driver version: 05.03.0010
उदाहरण