2

I've got third-party Python script, it looks like it has to connect to MySQL database by means of SQLObject package.

Considering I've provided correct DSN, the script throws

sqlobject.dberrors.OperationalError: Unknown database 'dbname?charset=utf8'

I've traced the problem to this piece of code

ar['charset'] = 'utf8'
conn = connectionForURI(uri, **ar)

which calls this function.

And it connects fine when ar['charset'] = 'utf8' is commented, so no query string is provided.

I have this issue on Windows,

  • MySQL 5.5.25
  • Python 2.7.2
  • MySQL-python 1.2.5
  • SQLObject 3.0.0a1dev-20150327

What exactly is going on there, and how it is supposed to be fixed? Does the problem lie in dependencies or the script itself?

Estus Flask
  • 206,104
  • 70
  • 425
  • 565

2 Answers2

2

I have done some research and found out that recent version of SQLObject uses the following code to extract connection parameters from URI. Unfortunately, urlparse function works that way so path that is used as DB name further parsed with a query string.

As a workaround for this issue, I could suggest passing DB encoding parameter explicitly to the connection object as follow:

conn = connectionForURI(uri)
conn.dbEncoding = 'utf-8'

It might help but it's worth to make a pull request to fix DB name extraction from URI.

UPD: Older version like 2.x uses a different code to parse connection URL which works well.

max
  • 2,757
  • 22
  • 19
  • Is that because SQLObject 3.x was installed instead of 2.x? – Estus Flask Dec 08 '15 at 14:59
  • I don't quite good in versioning of SQLObject but I don't think that this behaviour differs from 2.x. Anyway, you may try to check it using `git history`. Also, I found another function that parses URI and was written without `urlparse`. To use it you should call `connectionForURI` function with parameter `oldUri=False` – max Dec 08 '15 at 15:28
  • 1
    @estus My fault. Version 2.2 has different code to parse URI. Checking it. And it's work perfectly. Maybe you should use stable version. – max Dec 08 '15 at 15:48
  • Thank you for the research. Yes, 2.x works fine indeed, the one shouldn't rely on unconstrained package versions. – Estus Flask Dec 14 '15 at 22:34
1

Comma, not question mark:

db = MySQLdb.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASS,
      db=DB_NAME, charset="utf8", use_unicode=True)

If you can't get past the 3rd party software, abandon it.

Rick James
  • 135,179
  • 13
  • 127
  • 222
  • I can't demand the support from the author of the script, I can't abandon it as well. I believe this problem relates to the one of the listed components and the question is quite specific on that. – Estus Flask Dec 14 '15 at 22:19