...

View Full Version : Perl, DBI and czech (ISO-8859-2)



terrjie
05-03-2006, 09:44 AM
Hi everybody

I'm trying to set up Czech-language-support for a CMS:

I'm using MYSQL 4.1.11
DBI 1.50
perl 5.8.6

on a freeBSD server

my problem is everytime i insert or get some values with special czech-chars, i just get garbage (like č (c with this inverted "hat") turns to ? when updating and to ? when selecting). i've been trying and searching for several hours and i just can't get it working!

the collation of the table is set to "latin2_general_ci".

for testing I wrote this sample program:

#!/usr/bin/perl -w
use strict;
use DBI;
use CGI qw(:cgi);
use encoding 'latin2';
use Encode;
use vars qw($dbh);

DBI->trace(2);

my $test = encode("latin2", 'čar');

sqlDisconnect();
sqlConnect('DBI:mysql:testczech:localhost','user','pw');

sqlSimple("UPDATE tbl1 SET name='$test' WHERE ID='1'");

my ($test2) = sqlSelect('name','tbl1','ID=2');

print "$test2\n";

sqlDisconnect();


# SQL FUNCTIONS
# -----------------------------------------------------------------------------

sub sqlConnect {
my $dbname = shift || '';
my $dbusername = shift || '';
my $dbpassword = shift || '';

$dbh = DBI->connect($dbname, $dbusername, $dbpassword);

if (!$dbh) {

}

kill 9, $$ unless $dbh;
}

# -----------------------------------------------------------------------------

sub sqlDisconnect {

$dbh->disconnect if ($dbh);
$dbh = 0;
return 0;
}

# -----------------------------------------------------------------------------

sub sqlSimple {
my $sql = shift || '';

if (!$dbh->do($sql)) {
# ERROR

my $err=$dbh->errstr;
}
}

# -----------------------------------------------------------------------------

sub sqlSelect {
my $select = shift || '';
my $from = shift || '';
my $where = shift || '';
my $other = shift || '';

#debugMsg('sub', "sqlSelect($select, $from, $where, $other)");

my $sql="SELECT $select ";
$sql.="FROM $from " if $from;
$sql.="WHERE $where " if $where;
$sql.="$other" if $other;

#debugMsg('sql',"$sql");

my ($c)=$dbh->prepare(encode('iso-8859-2',$sql)) or die "Sql has gone to hell\n";

if(not ($c->execute())) {
# ERROR

my $err=$dbh->errstr;
debugMsg('sql', "ERROR -> $err");
return undef;
}
my (@r)=$c->fetchrow();
$c->finish();

return @r;
}

1;

can anybody give me some hints? would be GREATLY appreciated!!!

thanks
terrjie

nkrgupta
05-03-2006, 09:49 AM
Have you tried out the module CZ::Cstocs ?

http://search.cpan.org/~janpaz/Cstools-3.42/Cz/Cstocs.pm

See if it helps

terrjie
05-03-2006, 01:35 PM
Thx, but didn't really help. The problem is in the DBI-Module, cause everything is ok until the moment the script uses the DBI-Module to put / get something from the DB.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum