POST数据,GET数据,数据库查询数据(设置了编码),json转dict数据都是unicode字符串。
mysqldb
注:设置charset='utf8'1.中文 str 能正常写入
2.中文 unicode 能正常写入
1.英文 str 能正常写入
2.英文 unicode 能正常写入
1.中文 str|unicode 和 英文str 同时存在 能正常写入
2.中文 str 和 中文unicode 同时存在 报错
3.中文 str 和 英文unicode 同时存在 报错
1.查询 unicode
注:未设置charset='utf8'
1.中文 str 能正常写入
2.中文 unicode 报错
3.中文 str unicode 同时存在 报错
1.英文 str 能正常写入
2.英文 unicode 报错
2.查询 str
数据库
数据库入库时候charset = 编码 ,就可以直接插入unicode字符串入库。查询出来 和 POST过来的数据 都是unicode字符,设置编码 ,可以直接让unicode中文入库。
conn=MySQLdb.connect(host='localhost',user='root',passwd='',db='azheapp',port=3306,charset="utf8",cursorclass = MySQLdb.cursors.DictCursor)
文件
f = codecs.open('test.txt','r+','utf8'); 就可以直接插入unicode字符串写入文件。
用普通open,只能插入str字符串
django.db.connection
太强大了
不管字符串 是str还是unicode 或者同时存在 都能插入数据库