0

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:

$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.

Any thoughts on how I can get this to work?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
adeel825
  • 5,677
  • 12
  • 40
  • 44

3 Answers3

6

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.

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

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • I'm a unix noob - can you explain how I can confirm that my terminal is in UTF-8 and how I can update it? – adeel825 Dec 30 '08 at 22:03
  • your environment needs to be set (e.g. setenv LANG en_US.UTF-8 if you use "csh") and your terminal software needs to be set. That depends on what software you're running. – Alnitak Dec 30 '08 at 23:50
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.

converter42
  • 7,400
  • 2
  • 29
  • 24
0

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...

Community
  • 1
  • 1
Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373