gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:49:48 24 4
gpt4 key购买 nike

请找到以下错误的堆栈跟踪的最初几行:(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调用:

代码 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

代码一:我用来获取 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 格式。但如果这就是实际问题,那么为什么我会得到上述结果仅在少数情况下出错而不是每次都出错?请指教。

最佳答案

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

<小时/>

听起来确实与unicode有关。该列的字符集和排序规则是什么?请参阅INFORMATION_SCHEMA.COLUMNS看法。

就错误而言,我想这将取决于您要插入的字符(可能会改变)。严格遵守error message description ,即 er_truncated_wrong_value_for_field,听起来像是输入包含无效字符,或者输入字符串的解释被截断。同样,这听起来像是某种字符集问题。

更新:

假设您收到有效的 UTF8 字符串,这似乎确实是字符集问题。尽管我的测试数据库默认为字符集 UTF8,但我可以通过创建一个使用两种不同字符集的小表来重现该错误:LATIN1 和 UTF8。然后插入一个小UTF8 string进入两列。插入 UTF8 列工作正常,但 LATIN1 列失败并出现错误:

Incorrect string value: '/xD0/x9D/xD0/xB0 /xD0...' for column 'ColDefaultCharset' at row 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
  • 高级设置 > 连接字符串:characterEncoding=UTF8

关于mysql - 在coldfusion中处理MySQL NativeError Code 1366和SQLState HY000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26788570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com