[前][次][番号順一覧][スレッド一覧]

mysql:7708

From: Joel Rees <Joel Rees <joel@xxxxxxxxxx>>
Date: Wed, 14 May 2003 18:14:39 +0900
Subject: [mysql 07708] Re: why certain Japanese characters are getting displayed as wrong characters?

> 2. This raises a question.
> Fine we can solve the problem for '\'
> Are there any other characters which MySQL would not like?
> Do I have to take care of any other character(s)?

Look at the manual, section 6.1.1.1 on string literals:

http://www.mysql.com/doc/en/String_syntax.html

As far as I know, you should be escaping these characters (NUL \ ' ")
anyway, just before you pass them to MySQL, and un-escaping when you
read from MySQL.

> 3. I tried another solution. It seemed to work.
> I do not know how and why.
> here is what I did.
> 
> Instead of doing following
> 
> 	request.setCharacterEncoding ("Shift_JIS");
> 	String name = request.getParameter("name");
> 	String newName = insertSingleQuote(name);	// insert single quote around the
> string
> 	newName = replaceChar(newName, '\\', "\\\\");
> 	....
> 	String sql = "insert into test_table values (" + newName + ")";
> 	statement.executeUpdate(sql);
> 
> I DID FOLLOWING
> 
> 	//request.setCharacterEncoding ("Shift_JIS");	// do not need this now.

No automatic conversion from Unicode to sJIS.

> 	String name = request.getParameter("name");
> 	byte [] arrShift_JIS = name.getBytes("Shift_JIS");	// instead of using

Explicit conversion, instead.

> utf-8 string into the sql query directly use the byte array in shift_jis
> form

Do you understand that utf-8 is byte-style Unicode? In other words,
what you are putting into arrShift is not utf-8, but shift-JIS.

> 	PreparedStatement preparedStatement = connection.prepareStatement("insert
> into test_table values (?)");

Is that like a perl DBI placeholder? If so, my guess is that the
placeholder substitution is handling the escapes for you, which is
probably the best way to do it. That way, you don't need to think about
which characters need to be escaped.

> 	preparedStatement.setBytes(1, arrShift_JIS);
> 	preparedStatement.executeUpdate();
> 
> 
> Now this seems to solve the problem (possibly the JDBC driver/implementation
> will do appropriate escaping....)
> So my question to u guys is ...
> a. why does this work?
> b. is this a right solution? and
> c. will it take care of all the problematic characters?
> 
> regards,
> Haresh Gujarathi

-- 
Joel Rees <joel@xxxxxxxxxx>


[前][次][番号順一覧][スレッド一覧]

      7684 2003-05-12 17:36 ["eMantra Information] why certain Japanese characters are getting displayed as wrong characters?
      7686 2003-05-12 19:27 ┣[Masaki Ikeda <masaki]                                       
      7694 2003-05-13 16:02 ┃┗["eMantra Information]                                     
      7697 2003-05-13 16:27 ┃ ┗[Joel Rees <joel@xxxx]                                   
      7687 2003-05-12 19:45 ┗["Shimono Osamu" <shi]                                       
      7703 2003-05-14 15:47  ┗["eMantra Information]                                     
      7706 2003-05-14 17:27   ┣["Shimono Osamu" <shi]                                   
->    7708 2003-05-14 18:14   ┗[Joel Rees <joel@xxxx]