在coldfusion中处理MySQL NativeError Code 1366和SQLState HY000

时间:2014-11-06 20:13:31

标签: mysql stored-procedures coldfusion coldfusion-9 sendgrid

请在下面找到错误的最初几行堆栈跟踪: (列sl是我在存储过程代码中使用的那个)

Incorrect string value: '\xC2\x80\xC2\x99t ...' for column 'sl' at row 1 at 
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946):946 at 
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985):2985 at 
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631):1631 at 
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723):1723 at 
com.mysql.jdbc.Connection.execSQL(Connection.java:3283):3283 at 

and so on..

存储过程代码 CODE II ,将从下面提到的 CODE I 调用:

CODE II

-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`mystuff`@`%` PROCEDURE `usp_sg_ins_fv3`(IN `na` BIGINT, IN `sb` VARCHAR(200), IN `sc` INT, IN `se` INT, IN `sf` VARCHAR(200), IN `sg` VARCHAR(200), IN `sh` VARCHAR(500), IN `si` VARCHAR(200), IN `dj` DATETIME, IN `sk` VARCHAR(200), IN `sl` VARCHAR(500), IN `sm` VARCHAR(200)

, IN `sn` VARCHAR(50))
BEGIN  
    INSERT INTO sgfDatav3
                (
                 a_bi, 
                 b_vc,
                 c_int,
                 e_int,
                 f_vc, 
                 g_vc, 
                 h_vc,
                 i_vc,
                 j_dt,
                 k_vc,
                 l_vc,
                 m_vc,
                 n,
                 myTimestamp_dt
               )
            VALUES
                (
                na,
                sb,
                sc,
                se,
                sf,
                sg,
                sh,
                si,
                dj,
                sk,
                sl,
                sm,
                sn,
                CURRENT_TIMESTAMP()
                );
END

代码I: 我用来获取Sendgrid发布的数据的代码

<cftry>              
 <cfset incomingData = toString(getHttpRequestData().content) />
 <cfset djs = DeserializeJSON(incomingData)/>

<cfset a = "0">
<cfset b = "">
<cfset c = "0">
<cfset d = "0">
<cfset e = "">
<cfset f = "">
<cfset g = "">
<cfset h = "">
<cfset i = "">
<cfset k = "#NOW()#">
<cfset l = "">
<cfset m = "">
<cfset n = "">

<cfoutput>
<cfloop from="1" to="#arraylen(djs)#" index="i">

    <cfset a = "0">
    <cfset b = "">
    <cfset c = "0">
    <cfset d = "0">
    <cfset e = "">
    <cfset f = "">
    <cfset g = "">
    <cfset h = "">
    <cfset i = "">
    <cfset k = "#NOW()#">
    <cfset l = "">
    <cfset m = "">
    <cfset n = "">

    <cfif StructKeyExists(djs[i],'p')>
        <cfset a       = djs[i].p />
    </cfif>

    <cfif StructKeyExists(djs[i],'q')>
        <cfset b           = djs[i].q />
    </cfif>
    <cfif StructKeyExists(djs[i],'r')>
        <cfset c       = djs[i].r />
    </cfif>
    <cfif StructKeyExists(djs[i],'s')>
        <cfset d       = djs[i].s />
    </cfif>     
    <cfif StructKeyExists(djs[i],'t')>
        <cfset e       = djs[i].t />
    </cfif>
   <cfif StructKeyExists(djs[i],'u')>
        <cfset f           = djs[i].u />
    </cfif>
    <cfif StructKeyExists(djs[i],'v')>
        <cfset g       = djs[i].v />
    </cfif>

    <cfif StructKeyExists(djs[i],'w')>
        {                                                                         
        <cfset i  = djs[i].w />
        <cfset k  = dateAdd("s", i, createDateTime(1970, 1, 1, 0, 0, 0))/>
        }
    </cfif>
    <cfif StructKeyExists(djs[i],'x')>
        <cfset l         = djs[i].x />
    </cfif>
    <cfif StructKeyExists(djs[i],'y')>
        <cfset m       = djs[i].y />
    </cfif>
    <cfif StructKeyExists(djs[i],'z')>
        <cfset n       = djs[i].z /> 
    </cfif>

    <cfstoredproc procedure="sp1" datasource="db1">
        <cfprocparam cfsqltype="cf_sql_bigint" value="#a#">
        <cfprocparam cfsqltype="cf_sql_varchar" value="#left(b,199)#">
        <cfprocparam cfsqltype="cf_sql_integer" value="#c#">
        <cfprocparam cfsqltype="cf_sql_integer" value="#d#">
        <cfprocparam cfsqltype="cf_sql_varchar" value="#left(e,199)#">
        <cfprocparam cfsqltype="cf_sql_varchar" value="#left(f,199)#">
        <cfprocparam cfsqltype="cf_sql_varchar" value="#left(g,499)#">
        <cfprocparam cfsqltype="cf_sql_varchar" value="#left(h,199)#">
        <cfprocparam cfsqltype="cf_sql_timestamp" value="#k#">
        <cfprocparam cfsqltype="cf_sql_varchar" value="#left(l,199)#">
        <cfprocparam cfsqltype="cf_sql_varchar" value="#LEFT(m,499)#">
        <cfprocparam cfsqltype="cf_sql_varchar" value="#left(n,99)#">
        <cfprocparam cfsqltype="cf_sql_varchar" value="XX.XX.X.XX">
    </cfstoredproc>
</cfloop>

</cfoutput>

</cftry>

经过研究,我发现人们在谈论一些UTF8格式。但如果这是实际问题,那么为什么我会得到上述内容 只有少数情况的错误而不是每次都错误?请指教。

1 个答案:

答案 0 :(得分:2)

更新:从MySQL 5.5.3开始,还有UTF8mb4 which is often recommended over UTF8


听起来它与unicode有关。该专栏的字符集和校对是什么?请参阅INFORMATION_SCHEMA.COLUMNS视图。

至于错误是零星的,我想它将取决于你插入的确切字符(可能会改变)。严格地离开error message description,即er_truncated_wrong_value_for_field,听起来输入包含无效字符或输入字符串的解释被截断。再次,它听起来像某种charset问题。

<强>更新

假设您收到的是有效的UTF8字符串,它似乎确实是一个字符集问题。虽然我的测试数据库默认为charset UTF8,但我能够通过创建一个使用两个不同字符集的小表来重现该错误:LATIN1和UTF8。然后在两列中插入一个小UTF8 string。插入UTF8列工作正常,但LATIN1列失败并显示错误:

  

字符串值不正确:&#39; / xD0 / x9D / xD0 / xB0 /xD0...'对于专栏   &#39; ColDefaultCharset&#39;在第1行...

尝试将字符集更改为UTF8,我认为INSERT将正常工作:

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

表格

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;

示例文字

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

<强>程序:

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

<强>代码:

<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设置:

  • 驱动程序:MySQL 5
  • 高级设置&gt;连接字符串:characterEncoding=UTF8