插入数据时,DBD :: Pg对UTF8字符进行双重编码

时间:2017-03-12 21:19:57

标签: postgresql perl utf-8 dbi dbd-pg

我正在尝试使用DBD :: Pg将UTF8字符串插入到PostgreSQL中,并始终在数据库中获得双重编码的UTF8。如果我插入的字符串是Latin1。

,我只会在数据库中获得正确的字符

如何告诉DBD :: Pg不重新编码已经是UTF8的字符串?

测试脚本:

window.location = window.location.href+'&filename=file.file';

经过几次测试后得到的表格:

!/usr/bin/perl

use strict;

my $PGDB      = 'dbi:Pg:dbname=test;host=localhost';
my $PGDB_USER = 'username';    
my $SQL_INSERT = 'INSERT INTO tmp1 (t1, t2) VALUES (?, ?)';

use DBI;

my $dbh = DBI->connect($PGDB, $PGDB_USER)
    || die "Couldn't connect to $PGDB as user $PGDB_USER: $DBI::errstr\n";

#$dbh->do("SET client_encoding TO UTF8");

my $sth = $dbh->prepare( $SQL_INSERT )
    || die "Can't prepare insert statement $SQL_INSERT: $DBI::errstr";

my $cp1252 = "\xe9 voil\xe0";
my $utf8   = "é voilà";

utf8::upgrade($utf8);

use utf8;
#use bytes;

my $text = 'sent utf8 w. utf8::upgrade';

$sth->execute($utf8, $text) or die $sth->errstr, "\n";

(这是在Ubuntu 16.04上使用DBD :: Pg版本3.5.3。我在Ubuntu 12.04上安装的DBD :: Pg版本没有这个问题)

0 个答案:

没有答案