使用Perl从SQL Server检索数据时如何处理非ASCII字符?

时间:2022-06-12 15:31:04

I have a Perl script running on UNIX that uses DBI to connect to and retrieve data from a SQL Server database. The script looks like the following:

我有一个在UNIX上运行的Perl脚本,它使用DBI连接到SQL Server数据库并从中检索数据。该脚本如下所示:

$dbh = DBI->connect("dbi:Sybase:server=$connect;charset=UTF-8", $login, $password) or die("Couldn't connect to $connect as $login/$password:
$DBI::errstr");


$sql = "use mydb";
$sth = $dbh->prepare($sql);
$sth->execute or die("execute failed");
$sth->finish;


$sql = "MyProc \@DATE='1/1/2008'";
$sth = $dbh->prepare($sql);
$sth->execute or die("execute failed");
while (($body) = $sth->fetchrow()) {
        print "$body\n";
}
$sth->finish;

$dbh->disconnect if $dbh;

The body variable retrieves data from a column that is NVARCHAR and contains non-ASCII characters. The query runs fine, but the print statement spits out ????? when it encounters a non-ASCII character. In DBI->connect I even specify the character set, but no luck.

body变量从NVARCHAR列中检索数据并包含非ASCII字符。查询运行正常,但print语句吐出?????当遇到非ASCII字符时。在DBI-> connect我甚至指定字符集,但没有运气。

Any thoughts on how I can get this to work?

有关如何让它工作的任何想法?

3 个解决方案

#1


Your code looks OK.

你的代码看起来不错。

I've no reason to believe that what you're putting into the database and subsequently retrieving isn't still in UTF-8 encoding.

我没有理由相信你放入数据库并随后检索的内容仍然不是UTF-8编码。

Have you confirmed that the terminal on which your printing the data is actually in UTF-8 mode?

您确认打印数据的终端实际上​​是UTF-8模式吗?

#2


Oh how many hours I've wasted chasing non-existent bugs based on what I saw or didn't see when I printed data to my terminal. There are several different ways to verify your data that aren't affected by non-printing characters and don't depend on your system's display being able to map the correct glyphs to non-ASCII character codes. If your data don't look right dump them into a file and browse the file with a hex editor or run them through the od utility.

哦,根据我在终端上打印数据时看到或未看到的内容,我浪费了多少小时来追逐不存在的错误。有几种不同的方法可以验证不受非打印字符影响的数据,并且不依赖于系统的显示能够将正确的字形映射到非ASCII字符代码。如果您的数据看起来不正确,请将它们转储到文件中,然后使用十六进制编辑器浏览文件或通过od实用程序运行它们。

#3


I've connected Perl to SQL Server via FreeTDS + ODBC and have had no problem with character encodings. Maybe the Sybase DBI is the culprit here...

我已经通过FreeTDS + ODBC将Perl连接到SQL Server,并且对字符编码没有任何问题。也许Sybase DBI是罪魁祸首......

#1


Your code looks OK.

你的代码看起来不错。

I've no reason to believe that what you're putting into the database and subsequently retrieving isn't still in UTF-8 encoding.

我没有理由相信你放入数据库并随后检索的内容仍然不是UTF-8编码。

Have you confirmed that the terminal on which your printing the data is actually in UTF-8 mode?

您确认打印数据的终端实际上​​是UTF-8模式吗?

#2


Oh how many hours I've wasted chasing non-existent bugs based on what I saw or didn't see when I printed data to my terminal. There are several different ways to verify your data that aren't affected by non-printing characters and don't depend on your system's display being able to map the correct glyphs to non-ASCII character codes. If your data don't look right dump them into a file and browse the file with a hex editor or run them through the od utility.

哦,根据我在终端上打印数据时看到或未看到的内容,我浪费了多少小时来追逐不存在的错误。有几种不同的方法可以验证不受非打印字符影响的数据,并且不依赖于系统的显示能够将正确的字形映射到非ASCII字符代码。如果您的数据看起来不正确,请将它们转储到文件中,然后使用十六进制编辑器浏览文件或通过od实用程序运行它们。

#3


I've connected Perl to SQL Server via FreeTDS + ODBC and have had no problem with character encodings. Maybe the Sybase DBI is the culprit here...

我已经通过FreeTDS + ODBC将Perl连接到SQL Server,并且对字符编码没有任何问题。也许Sybase DBI是罪魁祸首......