Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read/write unicode data in MySql

I am using MySql DB and want to be able to read & write unicode data values. For example, French/Greek/Hebrew values.

My client program is C# (.NET framework 3.5).

How do i configure my DB to allow unicode? and how do I use C# to read/write values as unicode from MySql?

Upddate: 7 Sep. 09

OK, So my Schema, Table & columns are set to 'utf8' + collation 'utf8_general_ci'. I run the 'set names utf8' when the connection is opened. so far so good... but, still values are saved as '??????? '

any ideas?

The Solution!

OK, so for C# client to read & write unicode values, you must include in the connection string: charset=utf8

for example: server=my_sql_server;user id=my_user;password=my_password;database=some_db123;charset=utf8;

of course you should also define the relevant table as utf8 + collation utf8_bin.

like image 331
user123093 Avatar asked Sep 06 '09 15:09

user123093


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 add a Unicode character in MySQL?

In order to insert Unicode characters in MySQL, you need to create a table with Unicode support, select the appropriate encoding/collation settings, and specify the charset in the MySQL connection. Then, you can proceed and employ PHP code to insert Unicode as you please.

What is the difference between UTF-8 and utf8mb4?

The difference between utf8 and utf8mb4 is that the former can only store 3 byte characters, while the latter can store 4 byte characters. In Unicode terms, utf8 can only store characters in the Basic Multilingual Plane, while utf8mb4 can store any Unicode character.


3 Answers

The Solution!

OK, so for C# client to read & write unicode values, you must include in the connection string: charset=utf8

for example: server=my_sql_server;user id=my_user;password=my_password;database=some_db123;charset=utf8;

of course you should also define the relevant table as utf8 + collation utf8_bin.

like image 96
user123093 Avatar answered Oct 14 '22 09:10

user123093


try to use this query before any other fetch or send:

SET NAMES UTF8
like image 24
Mohammad Reza Hashemi Avatar answered Oct 14 '22 08:10

Mohammad Reza Hashemi


You have to set the collation for your MySQL schema, tables or even columns.

Most of the time, the utf8_general_ci collation is used because it is case insensitive and accent insensitive comparisons.

On the other hand, utf8_unicode_ci is case sensitive and uses more advanced sorting technics (like sorting eszet ('ß') near 'ss'). This collation is a tiny bit slower than the other two.

Finally, utf8_bin compares string using their binary value. Thus, it also is case sensitive.

If you're using MySQL's Connector/NET (which I recommend), everything should go smoothly.

like image 33
Bryan Menard Avatar answered Oct 14 '22 07:10

Bryan Menard