Dealing with MySQL NativeError Code 1366 and SQLState HY000 in coldfusion

喜你入骨 提交于 2019-12-02 11:50:00
Leigh

UPDATE: As of MySQL 5.5.3, there is also UTF8mb4 which is often recommended over UTF8.


It does sound like it is related to unicode. What is the charset and collation for that column? See the INFORMATION_SCHEMA.COLUMNS view.

As far as the error being sporadic, I imagine it would depend on exactly what characters you are inserting (which probably change). Going strictly off the error message description, ie er_truncated_wrong_value_for_field, it sounds like either the input contains invalid characters or the interpretation of the input string is being truncated. Again, it sounds like some sort of charset issue.

Update:

Assuming you are receiving a valid UTF8 string, it does seem to be a charset problem. Though my test database defaults to charset UTF8, I was able to reproduce that error by creating a small table that used two different charsets: LATIN1 and UTF8. Then inserting a small UTF8 string into the both columns. The insert into the UTF8 column worked fine, but the LATIN1 column failed with the error:

Incorrect string value: '/xD0/x9D/xD0/xB0 /xD0...' for column 'ColDefaultCharset' at row 1 ...

Try changing the charset to UTF8 and I think the INSERT will work correctly:

  ALTER TABLE YourTable MODIFY YourColumnName VARCHAR(500) CHARACTER SET utf8;

Table:

CREATE TABLE  TestTable (
  ID INTEGER NOT NULL AUTO_INCREMENT
  , ColDefaultCharset VARCHAR(100) CHARSET LATIN1 NULL
  , ColUTF8Charset VARCHAR(100) CHARSET UTF8 NULL
  , PRIMARY KEY (ID)
) ENGINE=InnoDB DEFAULT CHARSET=LATIN1;

Sample text:

На берегу пустынных волн
Стоял он, дум великих полн,

Procedures:

CREATE PROCEDURE `testWithUTF8`
(
  IN `sl` VARCHAR(500)
)
BEGIN
    INSERT INTO testTable (ColUTF8Charset)
    VALUES ( sl );

END


CREATE PROCEDURE `testWithLatin1`
(
  IN `sl` VARCHAR(500)
)
BEGIN
    INSERT INTO testTable (ColDefaultCharset)
    VALUES ( sl );

END

Code:

<cfprocessingdirective pageEncoding="UTF8">
<cfsavecontent variable="text">
На берегу пустынных волн
Стоял он, дум великих полн,
</cfsavecontent>

<!--- Note: For CF10, use cf_sql_nvarchar --->
<cfstoredproc procedure="testWithUTF8" datasource="MySQL" result="procResult">
    <cfprocparam cfsqltype="cf_sql_varchar" value="#text#">
</cfstoredproc>
<cfdump var="#procResult#">

<cfstoredproc procedure="testWithLatin1" datasource="MySQL" result="procResult">
    <cfprocparam cfsqltype="cf_sql_varchar" value="#text#">
</cfstoredproc>
<cfdump var="#procResult#">

DSN Settings:

  • Driver: MySQL 5
  • Advanced Settings > Connection String: characterEncoding=UTF8
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!