我的实现是这样的: Centos,Rails服务器的MySQL,目前正在开发一个新的瓶子应用程序。
我有一个数据库,我想分享的日期,在Rails和瓶子应用程序。我数据库中的一些数据是希腊文的。
#!/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)我的数据库中的记录是希腊文( id=1 )的一行,英文的( id=2 ),在连接期间没有设置字符集:在使用id=2时没有错误,使用id=1时得到这个错误。
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-8: ordinal not in range(128)像这样连接到DB:
plugin = bottle_mysql.Plugin(dbuser='user', dbpass='pass , dbname='db', charset='utf-8')我知道这个错误:
OperationalError: (2019, "Can't initialize character set utf-8 (path: /usr/share/mysql/charsets/)")有解决这种错误的办法吗?
更新:我将连接字符串上的字符集更改为“utf8”,并返回到第一个错误。
发布于 2015-05-14 07:41:28
row['first_name']是unicode,您不需要对它进行解码。
相反,打印时需要对其进行编码,如果没有,python解释器将为您完成此操作(例如,如果将其打印到stdout,python解释器将使用sys.stdout.encoding对其进行编码)。
我在您的代码中得到了相同的错误,但是在删除.decode("utf-8", "replace")之后,它将正常工作。
发布于 2015-05-11 06:10:06
核对表:
# -*- coding: utf-8 -*- -- (你有)charset='utf8' in connect() call --那是埋在bottle_mysql.Plugin里吗?(注:尝试“utf-8”和“utf8”)u'...'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />CHARACTER SET utf8 COLLATE utf8_general_ci ( MySQL )。参考文献:
https://stackoverflow.com/questions/30122898
复制相似问题