简体   繁体   English

将SQL文本文件导入MySQL时,Unicode转换为UTF-8编码问题

[英]Unicode to UTF-8 encoding issue when importing SQL text file into MySQL

Here's the scenario: 这是场景:

I have a url in a MySQL database that contains Unicode. 我在包含Unicode的MySQL数据库中有一个url。 The database uses the Latin-1 encoding. 数据库使用Latin-1编码。 Now, when I read the record from MySQL using Python, it gets converted to Unicode because all strings follow the Unicode format in Python. 现在,当我使用Python从MySQL读取记录时,由于所有字符串都遵循Python中的Unicode格式,因此记录转换为Unicode。

I want to write the URL into a text file -- to do so, it needs to be converted to bytes (UTF-8). 我想将URL写入文本文件-为此,需要将其转换为字节(UTF-8)。 This was done successfully. 这已成功完成。

Now, given the URLS that are in the text file, I want to query the db for these SAME urls in the database. 现在,给定文本文件中的URL,我想在数据库中查询这些SAME URL。 I do so by calling the source command to execute a few select queries. 我通过调用source命令来执行一些选择查询来做到这一点。

Result: I get no matches. 结果:我没有比赛。

I suspect that the problem stems from my conversion to UTF-8, which somehow is messing up the symbols. 我怀疑问题出在我转换为UTF-8的原因,这不知何故使符号混乱。

You most probably need to set your mysql shell client to use utf8. 您很可能需要将mysql shell客户端设置为使用utf8。

You can set it either in mysql shell directly by running set character set utf8 . 您可以通过运行set character set utf8在mysql shell中直接设置它。 Or by adding default-character-set=utf8 to your ~/.my.cnf . 或通过将default-character-set=utf8~/.my.cnf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM