参数化的SQL函数以特殊字符截断变量

问题描述 投票:1回答:1

我在ASP Classic中有一个用于参数化SQL条目的函数,大多数情况下都可以正常工作。问题在于它不能正确处理å,ä和ö等特殊字符。它只是切断那些字符所在的字符串。我已经在涉及代码页和字符集的所有ASP文件中设置了UTF-8,并且在数据库中我正在使用UTF8mb4_unicode_ci,在某些情况下使用UTF8mb4_swedish_ci。 UTF8应该可以按预期工作,并且此功能是目前唯一似乎无法处理的功能。我已经检查过,并且要发送的参数在实际执行之前和之后都正确包含了函数内部的字符。

这是我正在使用的功能:

function dbPara(sqlPara, params)
dim cmd
set cmd = Server.CreateObject("ADODB.Command")
with cmd
    .CommandText = sqlPara
    set .ActiveConnection = cn
end with

if NOT isEmpty(params) then
    set rsPara = cmd.execute(, params)
else
    set rsPara = cmd.execute()
end if

response.write(params(0)) 'For test purpose only, this will output the entire string.

set dbPara = rsPara
end function

这是使用该函数无法正常工作的示例的代码,并且在未参数化的情况下可以工作的示例的代码:

Set cn = Server.CreateObject("ADODB.Connection")
cn.Open "Driver={MySQL ODBC 8.0 Unicode driver};Database="&db&";Server="&dbserver&";UID="&dbuser&";PWD="&dbpassword&""

comment = "This is a test to see if å is working properly."
sqlA = "UPDATE orders SET orders_comment = ? WHERE orders_id = 1234"
call dbPara(sqlA, array(comment))
'This will only set the orders_comment to everything up to the å

cn.execute("UPDATE orders SET orders_comment = '" & comment & "' WHERE orders_id = 1234)
'This will set the orders_comment correctly to the whole string

有人可以看到此功能引起此行为的任何潜在问题吗?

编辑:添加一个具有多个条目的示例,以解释我的评论,即第一个变量的特殊字符用?代替,但第二个变量的条目被切断。这是当Lankymark建议的charset=utf8;不起作用时,我将连接字符串设置为使用charset=ucs2;的情况:

    langSQL = "UPDATE faq_entry_lang SET faq_entry_lang_title = ?, faq_entry_lang_content = ? WHERE faq_entry_lang_entryID = ? AND faq_entry_lang_lang = ?"
    call dbPara(langSQL,array(faqtitle, faqcontent, entryid, lang))

编辑2:经过更多测试。中断和未在连接字符串中使用utf8时,似乎是随机的。所有未截断的特殊字符将变为?不过。

mysql utf-8 asp-classic odbc
1个回答
0
投票

请提供SHOW CREATE PROCEDURE name

然后看最后-它会告诉您创建proc(或CHARACTER SET)时COLLATIONFUNCTION的作用。

我怀疑这不是您所需要的。要修复:

DROP PROCEDURE name;
SET NAMES utf8mb4 COLLATE utf8mb4_unicode_520_ci;  -- (or whatever)
DELIMITER //
CREATE PROCEDURE
    ...... ;
//
DELIMITER ;

更多讨论:在Trouble with UTF-8 characters; what I see is not what I stored中搜索“截断”

© www.soinside.com 2019 - 2024. All rights reserved.