我决定在python3中首次使用django1.7。
我需要能够使用包含utf8数据的遗留latin1数据库。我知道这很糟糕,但是数据库太大了,根本不可能改变这一点。所以我试着:DATABASES = {
'ENGINE' : 'django.db.backends.mysql', // using MySQL-python fork with support for py3
...
'OPTIONS' : {
'init_command': "SET character_set_results = 'latin1'",
#'read_default_file': '/etc/my.cnf.d/client.cnf', // I've also tried this one
}
}
我还尝试了Oracle中的pythonmysql连接器,其设置如下
^{pr2}$
/等/我的.cnf.d/客户端.cnf在[client]
init-command='SET character_set_results = "latin1"'
# password, host, username
在这两种情况下,我都可以连接到数据库,但看起来Django将character_set_results设置回utf8。在
我试着跟着from django.db import connection
with connection.cursor() as c:
// I expect variable to be 'latin1'
c.execute("show variables like 'character_set_results%'")
c.fetchone() // returns ('character_set_results', 'utf8')
// here I try to set it manually
c.execute("SET character_set_results = 'latin1'")
c.execute("show variables like 'character_set_results%'")
c.fetchone() // returns ('character_set_results', 'latin1') // now it's OK我确信django使用了client.cfg文件并更正了[section],因为它包含用户名/密码,并且成功地连接到数据库
当我在使用相同配置文件的linux终端中使用mysql命令时,一切正常
所以我猜Django以某种方式强制character_set_results变量为utf8。有可能吗?我有什么办法可以解决这个问题吗?在
非常感谢