Search code examples
pythonmysqlutf-8character-encodingbottle

bottle_mysql Encoding failure


My implementation goes like this : Centos Server, MySQL with Rails server and currently working on a new bottle application on that.

I have a database that i want to share the date in both Rails and Bottle app. Some data in my DB are in greek.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import bottle
import bottle_mysql

app = bottle.Bottle()
# dbhost is optional, default is localhost
plugin = bottle_mysql.Plugin(dbuser='user', dbpass='pass' , dbname='db')
app.install(plugin)

@app.route('/show/<item>')
def show(item, db):
    db.execute('SELECT * from visitors where id=1')
    row = db.fetchone()
    if row:
        print row['first_name'].decode("utf-8", "replace")
        return template('showitem', page=row)
    print "Empty"
    return HTTPError(404, "Page not found")

app.run(host='domain.tld', port=8080)

The record in my DB are one row in greek (id=1) and one in english (id=2) Without setting the charset during the connection: I get no errors when using id=2 I get this error when using id=1

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-8: ordinal not in range(128)

Connecting to the DB like that :

plugin = bottle_mysql.Plugin(dbuser='user', dbpass='pass , dbname='db', charset='utf-8')

I get this error :

OperationalError: (2019, "Can't initialize character set utf-8 (path: /usr/share/mysql/charsets/)")

Any workaround on this kind of errors ?

UPDATE : I changed the charset on my connection string into 'utf8' and went back to the first error.


Solution

  • row['first_name'] is unicode, you don't need to decode it.

    In contrast, you need to encode it when printing, and if you not, python interpreter will do it for you (For example if you are printing it to the stdout, python interpreter will encode it with sys.stdout.encoding).

    I got the same error in your code, but it will work fine after I remove .decode("utf-8", "replace")