2013-07-08 36 views
1

下面是示例代码:MySQL的插入和Unicode字符

CREATE DATABASE test_unicode DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; 

USE test_unicode; 

CREATE TABLE simple_table (text varchar(100) NOT NULL); 

INSERT INTO simple_table VALUES ('l\u2019agave'); 

SELECT * FROM simple_table; 

这里是输出:

+-------------+ 
| text  | 
+-------------+ 
| lu2019agave | 
+-------------+ 
1 row in set (0.00 sec) 

MySQL存储我的字符串,不 - “\

我需要用“ - ”\

我该怎么做?

我尝试了很多的编码设置到MySQL,但它不是为我工作...

感谢您的建议!

+0

使用\\双逃逸 – DevZer0

回答

4

尝试

INSERT INTO simple_table VALUES ('l\\u2019agave'); 
+0

感谢。它正在工作......但我从postgres(使用pg_dump)得到了“l \ u203”,我将需要替换掉所有的字符串......也许还有其他方法吗? – Tom

+2

@AlexeyLisikhin你可以尝试使用[NO_BACKSLASH_ESCAPES SQL模式](http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_no_backslash_escapes)'SET SESSION sql_mode = NO_BACKSLASH_ESCAPES' – bansi

+0

非常感谢你!它正在为我工​​作 – Tom

1

试试这个:

INSERT INTO `test_unicode`.`simple_table` (`text`) VALUES ('l\\u2019agave'); 

This is because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\n”. To search for “\”, specify it as “\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against