Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

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

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.

like image 854
Rocky Pulley Avatar asked Jul 25 '11 16:07

Rocky Pulley


People also ask

Does MySQL support Unicode?

MySQL supports these Unicode character sets: utf8mb4 : A UTF-8 encoding of the Unicode character set using one to four bytes per character. utf8mb3 : A UTF-8 encoding of the Unicode character set using one to three bytes per character.

How do I insert text into a MySQL database?

INSERT INTO Syntax It is possible to write the INSERT INTO statement in two ways: 1. Specify both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...)

How do I insert something into MySQL?

First, you must specify the name of the table. After that, in parenthesis, you must specify the column name of the table, and columns must be separated by a comma. The values that you want to insert must be inside the parenthesis, and it must be followed by the VALUES clause.


2 Answers

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.

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/string-syntax.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');
like image 77
Crewe Avatar answered Sep 26 '22 02:09

Crewe


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:

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

My db uses default encoding (latin1).

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

like image 28
Bohemian Avatar answered Sep 24 '22 02:09

Bohemian