使用 Perl DBD 从 MS SQL Server 获取数据时的特殊字符处理
我有一个 MS SQL Server 2008 数据库,我使用 perl DBD::Sybase 模块从中获取数据。但数据库中有一些特殊字符,如版权符号、商标符号等,无法正确导入。 Perl 似乎将所有这些特殊字符都更改为问号字符。有办法解决这个问题吗?
我尝试在连接字符串中指定 charset=utf8
。 doc 提到了 syb_enable_utf8 (bool)< /code> 设置,但每当我尝试这样做时,都会收到错误:
Can't locate object method "syb_enable_utf8" via package "DBI::db"
I have an MS SQL Server 2008 Database, from which I am fetching data using perl DBD::Sybase module. But there are some special characters in the DB, like the Copyright symbol, Trademark symbol etc., which are not getting imported properly. Perl seems to change all of these special characters to a Question mark character. Is there a way to fix this?
I have tried specifying charset=utf8
in the connection string. The doc mentions a syb_enable_utf8 (bool)
setting, but whenever I try that, I get an error:
Can't locate object method "syb_enable_utf8" via package "DBI::db"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现的一个解决方案是:
然后,无论您将数据写入文件还是其他任何地方,请使用
Encode::encode_utf8($data);
其中
$data
是您从 MSSQL 获取的列/值。One solution I found was this:
Then, wherever you are writing data to a file or anywhere else, use
Encode::encode_utf8($data);
where
$data
is the column/value which you have fetched from MSSQL.我不使用 DBD::Sybase,但是 a) 我使用很多其他 DBD,b) 我目前正在收集有关 DBD 中 unicode 支持的信息。根据 pod,使用 syb_enable_utf8 时至少需要 OpenClient 15.x。您使用的是 15.x 或更高版本吗?如果您的客户端低于 15.x,或者您的 DBD::Sybase 版本太旧,则可能未定义 syb_enable_utf8。不幸的是,我无法从更改文件中看到添加 syb_enable_utf8 的时间。
但是,当您说“无法找到方法”时,我认为这是一条线索,因为 syb_enable_utf8 不是方法,而是 pod 中的一个属性(位于 Sybase 特定属性下)。因此,您需要将其添加到您的 connect 调用中或通过连接句柄进行设置,如下所示:
或者
您还应该阅读 pod 中描述设置 syb_enable_utf8 时发生的情况的位,因为它仅适用于 UNIVARCHAR、UNICHAR 的文档中显示和 UNITEXT 列。
最后,您需要确保首先正确插入数据。我猜想如果它不是从 Perl 中使用 syb_enable_utf8 和 charset=utf8 插入的,并且在插入之前您的数据不是 Perl 中正确的 unicode 字符,您将得到垃圾。
Raze2dust 所做的评论与您的问题无关,但如果您打算将从数据库检索到的数据写入其他地方,则值得留意。只需记住对脚本中的所有数据输入进行解码并对所有数据输出进行编码即可。
I don't use DBD::Sybase but a) I use a lot of other DBDs and b) I am currently collecting information about unicode support in DBDs. According to the pod you need at least OpenClient 15.x when using syb_enable_utf8. Are you using 15.x or later? Perhaps syb_enable_utf8 is not defined if your client is less than 15.x or perhaps you have too old a version of DBD::Sybase. Unfortunately I cannot see from the Changes file when syb_enable_utf8 was added.
However, when you say "can't locate method" I think that is a clue as syb_enable_utf8 is not a method, it is an attribute (it is under Sybase Specific Attributes) in the pod. So you need to add it to your connect call or set it via a connection handle like this:
or
You should also read the bits in the pod describing what happens when syb_enable_utf8 is set as it appears from the documents it only applies to UNIVARCHAR, UNICHAR, and UNITEXT columns.
Lastly, you need to ensure you insert the data correctly in the first place. I'd guess if it is not inserted from Perl with syb_enable_utf8 and charset=utf8 and your data is not proper unicode characters in Perl before you insert you'll get garbage back.
The comment Raze2dust made had nothing to do with your issue but is worth heeding if you are going to write the data retrieved from your database elsewhere. Just remember to decode any data input to your script and encode any data output.