无法将character_set_results设置为latin1 [英] Can't set character_set_results to latin1
问题描述
我需要能够使用包含
latin1
数据库的 utf8
数据。我知道它很烂,但数据库是如此巨大,这是真的不可能改变这一点。所以我尝试以下: DATABASES = {
'ENGINE':'django.db.backends.mysql' //使用MySQL-python fork支持py3
...
'OPTIONS':{
'init_command':SET character_set_results ='latin1',
# read_default_file':'/etc/my.cnf.d/client.cnf',//我也试过这个
}
}
我还从Oracle中尝试过python-mysql-connector,具有以下设置
code> DATABASES = {
'ENGINE':'mysql.connector.django',//使用MySQL-python fork支持py3
'OPTIONS':{
'option_files ':['/etc/my.cnf.d/client.cnf'],
}
}
/etc/my.cnf.d/client.cnf
[客户端]
init-command ='SET character_set_results =latin1'
#password,host,username
<在这两种情况下我可以连接到数据库,但似乎Django将character_set_results设置为utf8。
我尝试过以下
从django.db import connection
with connection.cursor()as c:
//我希望变量为'latin1'
c.execute(show variables like'character_set_results%')
c.fetchone()//返回('character_set_results','utf8')
//这里我尝试手动设置
c.execute(SET character_set_results ='latin1')
c.execute(show variables like'character_set_results%')
c.fetchone()/ / returns('character_set_results','latin1')//现在没关系
- 当我在linux终端中使用
mysql
命令同样的配置文件,一切都按预期工作
<我确定django使用
client.cfg
文件并更正 [section]
,因为它包含用户名/密码并且它成功连接到数据库 所以我猜Django以某种方式执行 character_set_results
变量为 utf8
。可能吗?有什么办法可以解决这个问题吗?
非常感谢你
我终于明白了(我不知道为什么我在发布之后总是找到一个解决方案)。
from django.db.backends.signals import connection_created
def connection_setup(** kwargs):
conn = kwargs ['connection']
with conn.cursor ()作为游标:
cursor.execute(SET character_set_results ='latin1')
cursor.close()
我以前用Oracle的 python-mysql-connector
尝试过,并且它抛出
RuntimeError:相比之下超过最大递归深度
但它适用于 MySQL驱动程序
py3分支。我想这可能是我要报告的 python-mysql-connector
或 Django
中的错误。也许这会帮助某人。
I've decided to use Django 1.7 for the first time with Python 3.
I need to be able to work with legacy latin1
database which contains utf8
data. I know it sucks, but the database is so huge that it's really impossible to change this. So I tried following:
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
}
}
I've also tried python-mysql-connector from Oracle with following setup
DATABASES = {
'ENGINE' : 'mysql.connector.django', // using MySQL-python fork with support for py3
'OPTIONS' : {
'option_files': ['/etc/my.cnf.d/client.cnf'],
}
}
/etc/my.cnf.d/client.cnf
[client]
init-command='SET character_set_results = "latin1"'
# password, host, username
In both cases I can connect to database, but it seems like Django sets character_set_results back to utf8.
I've tried following
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
- I'm sure django uses
client.cfg
file and correct[section]
, because it contains username/password and it successfully connects to the database - When I use
mysql
command in the linux terminal which uses the same configuration file, everything works as expected
So I guess Django somehow enforces character_set_results
variable to be utf8
. Is it possible? Is there any way how can I solve this issue?
Thank you very much
I finally figured it out (I don't know why I always find a solution a while after posting it to SO)
from django.db.backends.signals import connection_created
def connection_setup(**kwargs):
conn = kwargs['connection']
with conn.cursor() as cursor:
cursor.execute("SET character_set_results = 'latin1'")
cursor.close()
I've tried it before with Oracle's python-mysql-connector
and it threw
RuntimeError: maximum recursion depth exceeded in comparison
but it works with MySQL-driver
py3 branch. I guess it can be a bug in python-mysql-connector
or Django
which I'll report. Maybe this will help somebody.
这篇关于无法将character_set_results设置为latin1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!