7

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.

Rocky Pulley
  • 22,531
  • 20
  • 68
  • 106

6 Answers6

8

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');
Eric Darchis
  • 24,537
  • 4
  • 28
  • 49
Crewe
  • 303
  • 2
  • 8
  • I can make it either, but in both, it inserts the text "u5c40u5c42" instead of the two unicode characters. – Rocky Pulley Jul 25 '11 at 17:34
  • Also, let me clarify that I can insert and select unicode data just fine using java, but I want to be able to paste some insert statements into the mysql client and want to know how to format them. – Rocky Pulley Jul 25 '11 at 17:35
  • What version of MySQL are you using? – Crewe Jul 25 '11 at 18:16
  • 1
    If you have => 5.5 then try `INSERT INTO table VALUES (1, 12, _ucs2'\x5C40\x5C42');` – Crewe Jul 25 '11 at 18:23
6

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?

Dale K
  • 25,246
  • 15
  • 42
  • 71
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

By using MySQL Workbench

  1. Alter the table of that column you want to insert unicode into.
  2. Change Collation of that column to utf8-default collation.
  3. Apply the setting and you are good to go to insert unicode.

MySQL Workbench Screenshot

Binaya Shrestha
  • 440
  • 3
  • 12
0

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

If you are doing something with php then take a look at this link 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.

0

In my case, I needed to insert Arabic characters into MySql server through C# form application. The only way that worked for me is as follows:

First: In your code, specify character set in the connection string as follows:

MySqlConnection mysqlConn = new MySqlConnection("Server= serverName; Port=3306; Database= dbName;  Uid = userName; Pwd=password; charset=utf8");

Second: In phpMyAdmin console, click on your database name link, then head to "operations" tab and go to "Collation" at the bottom and select "utf8_unicode_ci" and check the options below and finally click on "Go"

Steps here

Ahmed Ali
  • 1
  • 2
0

this worked for me

$con=mysqli_connect("localhost","my_user","my_password","my_db");

// Change character set to utf8
mysqli_set_charset($con,"utf8");
infantry
  • 336
  • 1
  • 5
  • 15