MySQL不能正确存储一些UTF8字符 [英] MySQL does not store properly some UTF8 chars

查看:323
本文介绍了MySQL不能正确存储一些UTF8字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 mysql dbms来存储来自维基百科的页面。我在my.cnf文件中使用指令将字符集编码设置为 utf-8 (维基百科编码):

  [mysqld] 
character_set_server = utf8

并使用'chararacter set utf8'属性定义创建了我的数据库。



charset-encoding for mysqld client by:




  • 插入'charSet = utf8'
  • 执行查询'set names utf8'


但是我注意到mysql服务器用其他字符替换了一些字符。



例如,它用 a 替换á / p>

UPDATE



我已执行命令 show例如'%char%'的变量,确保 character_set_client character_set_set $ c> utf8 。



如何在我的数据库中存储正确的字符?

解决方案

尝试在DB URL中指定编码:

  url =jdbc:mysql:// localhost:port / DBNAME?characterEncoding = UTF-8

这里有一些关于我的答案的信息:



以下是从MySQL文档( http://dev.mysql.com/doc/refman/5.0/en/ connector-j-reference-charsets.html ):


从JDBC驱动程序发送到服务器的所有字符串都转换为
自动从本地Java Unicode表单到客户端字符
编码,包括使用Statement.execute(),
发送的所有查询Statement.executeUpdate(),Statement.executeQuery()以及所有
PreparedStatement和CallableStatement参数,使用setBytes(),setBinaryStream(),
setAsciiStream(),setUnicodeStream()和setBlob()设置的参数排除



设置字符编码

在连接时会自动检测客户端
和服务器之间的字符编码。在服务器上使用character_set_server为服务器
版本4.1.0及更高版本指定
编码。驱动程序自动使用服务器指定的编码
。要覆盖客户端上自动检测到的
编码,请使用用于连接到服务器的
URL中的characterEncoding属性。要允许从客户端发送多个字符集
,请使用UTF-8编码,通过
将utf8配置为默认服务器字符集,或通过
配置JDBC驱动程序使用UTF-8通过characterEncoding
属性。


我在几个月前遇到了类似的问题。我检查我的MySQL(使用mysqld --verbose -help命令)的character_set_server的默认值。
它是latin1。


I'm using the mysql dbms to store pages from Wikipedia. I've set the character-set encoding to utf-8 (wikipedia encoding) in my.cnf file with the directive:

[mysqld]
character_set_server = utf8

And created my database with the 'chararacter set utf8' property definition.

I've also changed the charset-encoding for mysqld client by:

  • inserting the 'charSet=utf8' property when initializing my jdbc driver.
  • doing a query to 'set names utf8'

However I've noticed that mysql server replaces some characters with others.

For example it replaces á with a.

UPDATE

I've run the command show variables like '%char%' ensuring that both character_set_client and character_set_set are utf8.

How can I store the correct chars in my db? Thanks!

解决方案

Try to specify the encoding in the DB URL :

url="jdbc:mysql://localhost:port/DBNAME?characterEncoding=UTF-8"

Here's some more information regarding my answer :

The following is taken from the MySQL documentation (http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-charsets.html) :

All strings sent from the JDBC driver to the server are converted automatically from native Java Unicode form to the client character encoding, including all queries sent using Statement.execute(), Statement.executeUpdate(), Statement.executeQuery() as well as all PreparedStatement and CallableStatement parameters with the exclusion of parameters set using setBytes(), setBinaryStream(), setAsciiStream(), setUnicodeStream() and setBlob().

Setting the Character Encoding
The character encoding between client and server is automatically detected upon connection. You specify the encoding on the server using the character_set_server for server versions 4.1.0 and newer. The driver automatically uses the encoding specified by the server. To override the automatically detected encoding on the client side, use the characterEncoding property in the URL used to connect to the server. To allow multiple character sets to be sent from the client, use the UTF-8 encoding, either by configuring utf8 as the default server character set, or by configuring the JDBC driver to use UTF-8 through the characterEncoding property.

I encountered a similar problem a few months ago. I checked the default value of character_set_server on my MySQL (using the "mysqld --verbose –help" command). It was latin1.

这篇关于MySQL不能正确存储一些UTF8字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆