如何使用插入语句将 unicode 字符插入到 mysql 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6819679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:36:34  来源:igfitidea点击:

how do I insert unicode characters into mysql with an insert statement?

mysql

提问by Triton Man

I'm doing this directly in the mysql client. I want to do the following:

我直接在 mysql 客户端中执行此操作。我想做以下事情:

INSERT INTO MYTABLE VALUES(1,12,'\u5c40\u5c42');

So it would insert the two unicode characters. I'd like to do this without using some other programming language if possible, I'd like to just paste my insert statements right into mysql client.

所以它会插入两个unicode字符。如果可能的话,我想在不使用其他编程语言的情况下执行此操作,我只想将插入语句直接粘贴到 mysql 客户端中。

采纳答案by Bohemian

Using the mysql console, I can just paste your unicode characters into an insert command and mysql accepts it. Here's a little test I did using your data:

使用 mysql 控制台,我可以将您的 unicode 字符粘贴到插入命令中,然后 mysql 接受它。这是我使用您的数据进行的一个小测试:

CREATE TABLE xx (col1 varchar(20));
insert into xx values ('局层');
select * from xx;
+---------+
| col1    |
+---------+
| 局层   |
+---------+

My db uses default encoding (latin1).

我的数据库使用默认编码(latin1)。

Have you tried just pasting them in? What error, if any, do you get?

你试过把它们粘贴进去吗?如果有的话,你会得到什么错误?

回答by Crewe

What's the type of your table data? char or varchar? Your issue isn't quite clear, are you getting an error from that line? You might be experiencing: http://dev.hubspot.com/bid/7049/MySQL-and-Unicode-Three-Gotchas.

你的表数据的类型是什么?字符或varchar?您的问题不太清楚,您是否从该行收到错误消息?您可能会遇到:http: //dev.hubspot.com/bid/7049/MySQL-and-Unicode-Three-Gotchas

EDIT:

编辑:

Quite a bit of information is within these three pages that should be able to help: http://dev.mysql.com/doc/refman/5.5/en/charset-unicode.html

这三个页面中有相当多的信息应该能够提供帮助:http: //dev.mysql.com/doc/refman/5.5/en/charset-unicode.html

http://dev.mysql.com/doc/refman/5.5/en/string-syntax.html

http://dev.mysql.com/doc/refman/5.5/en/string-syntax.html

http://dev.mysql.com/doc/refman/5.5/en/charset-literal.html

http://dev.mysql.com/doc/refman/5.5/en/charset-literal.html

but I also saw this :

但我也看到了这一点:

INSERT INTO mytable VALUES (1, 12, _ucs2'\x5C40\x5C42');

回答by Binaya Shrestha

By using MySQL Workbench

通过使用 MySQL Workbench

  1. Alterthe table of that column you want to insert unicode into.
  2. Change Collationof that column to utf8-default collation.
  3. Applythe setting and you are good to go to insert unicode.
  1. Alter要插入 unicode 的那一列的表。
  2. Collation将该列更改为utf8-default collation.
  3. Apply设置,你很高兴去插入 unicode。

MySQL Workbench Screenshot

MySQL 工作台屏幕截图

回答by Brian - TechToolbox

It will depend on what you are programming with or if just dealing with the database directly. Are you just trying to do a straight insert from querybrowser or some other tool or are you doing this via a web app. IF the second, what language is the webapp using. If it is a .net app you can try setting the character set in the connection string i.e. charset=utf8

这将取决于您正在使用什么编程或是否直接处理数据库。您是只是想从查询浏览器或其他工具中直接插入,还是通过 Web 应用程序执行此操作。如果是第二个,webapp 使用什么语言。如果是 .net 应用程序,您可以尝试在连接字符串中设置字符集,即 charset=utf8

If you are doing something with php then take a look at this link http://randomchaos.com/documents/?source=php_and_unicode

如果你正在用 php 做一些事情,那么看看这个链接http://randomchaos.com/documents/?source=php_and_unicode

You could also go and set the default character set on the database to UTF-8. Not sure how this will impact the current data so be sure to backup everything before making changes to the database.

您还可以将数据库上的默认字符集设置为 UTF-8。不确定这将如何影响当前数据,因此请务必在更改数据库之前备份所有内容。