【问题标题】:How can I read special characters from a table of SQLSERVER in perl?如何从 perl 中的 SQLSERVER 表中读取特殊字符?
【发布时间】:2019-10-05 06:44:43
【问题描述】:

使用 perl 我正在尝试从具有默认字符集 = Latin1_General_CI_AS 的 SQLSERVER 数据库中读取一些记录

这些记录有一些特殊字符(例如:à è ì òù)

当我提取这些记录时,perl 输出不显示这些特殊字符。

我尝试了编码/解码功能,但没有成功...

use DBI;
# DBD::ADO 
$dsn = "Provider=sqloledb;Trusted Connection=yes;"; 
$dsn .= "Server=$host;Database=$database"; 
my $dbh = DBI->connect("dbi:ADO:$dsn",  $user,  $auth, 
 { RaiseError => 1, AutoCommit => 1} 
 ) || die "Database connection not made: $DBI::errstr";

$sql = "select idnews, titolo from news where idnews>$ultimoidinserito";

 $sth = $dbh->prepare($sql);
 $sth->execute
 or die "SQL Error: $DBI::errstr\n";
  while(@row = $sth->fetchrow_array) 
  {
    $titolo=lc($row[1]);
    $idnews=$row[0];
        print "$titolo\n";
 }

预计会有一些记录:

Eurolega la finale sarà CSKA-Efes

Futuro Allegri ecco dove può andare

得到的结果:

Eurolega la finale sarα CSKA-Efes

Futuro Allegri ecco dove pu≥andare

【问题讨论】:

  • 1) 大多数 DBD 返回字符串encoded,所以由你来解码它们。从文档中可以看出,您可以使用Win32::OLE->Option( CP => Win32::OLE::CP_UTF8 ); 将编码设置为 UTF-8,以便您轻松解码。
  • 2) 无需先对输出进行编码即可打印到终端。
  • 关于后一点,见this
  • 很好的答案@ikegami7!问题解决了!为标准输出添加编码就足够了:require Win32;我的 $coe = "cp" 。 Win32::GetConsoleOutputCP(); binmode(STDOUT, ":encoding($coe)");非常感谢你

标签: sql-server perl utf-8 character-encoding iso-8859-1


【解决方案1】:
my $reportJson = to_json($data,{ pretty => 1})

使用 perl “漂亮”,一切看起来都不错。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-08
    • 2011-03-10
    • 2021-05-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多