Sachin Aryal
Sachin Aryal

Reputation: 801

pyodbc + MySQL ODBC cannot deal with Unicode database name

I am using

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)")

Upvotes: 2

Views: 1373

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123539

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
उदाहरण

Upvotes: 1

Related Questions