RE: :ODBC & FreeTDS: problem binding parameter as SQL_WCHAR or SQL_WVARCHAR

From: Frediano (Frediano.Ziglio_at_vodafone.com)
Date: 09/20/04


Date: Mon, 20 Sep 2004 11:14:47 +0200
To: "Konstantin Khomoutov" <khomoutov@despammed.com>, <dbi-users@perl.org>


>
> I need to access MS SQL server from CGI scripts running on Linux box.
>
> Here in Russia we have a pain using 2 different
> charsets for Cyrillic: CP1251 on Windows and KOI8-R on Unices.
> So all the data bases on MS SQL server have data in CP1251,
> but CGI scripts use KOI8-R.
>
> Transferring data from SQL server to CGI scripts is handled in
> a very simple manner: FreeTDS converts UCS-2LE from SQL server to
> KOI8-R according to the value of "client charset" in its config file.
> So all SELECT statements work perfectly.
>
> The problem is with parameterized UPDATE/INSERT statements:
> when we do something like
>
> $sth->prepare('INSERT INTO MYTABLE (MYFIELD) VALUES (?)');
> $sth->exec('plain KOI8-R text here');
>
> the parameter for the placeholder is passed through FreeTDS
> to the SQL server as is (this can be found in a FreeTDS trace file).
>
> FreeTDS documentation says that conversion is performed on data
> of types nchar, nvarchar and ntext (Unicode types in MS SQL).
> So I've changed types of the string fields in my database tables
> to "n...", but the parameter values continued to pass unconverted.
>
> Then I've decided to force DBI (or whatever?) treat my parameter as
> being destined to Unicode column:
>
> $sth->prepare(...);
> my $param = 'plain KOI8-R text string';
> $sth->bind_param(1, $param, { TYPE=>SQL_WVARCHAR });
> $sth->execute;
>
> But this fails in DBD::ODBC while trying to perform "execute"
> (please, see the log recorded using DBI_TRACE=2 in the attach).
>
> I'm new to DBI so I don't know at which level this error is actually
> occur. Or may be I just don't understand how one should handle such
> a case with different charsets?
>
> P.S.
> 1. For now we use Text::Iconv to explicitly convert each parameter
> to the SQL server encoding.
> 2. Making SQL statements with hard-coded parameters is no way: we need
> them prepared and executed multiple times with different parameters.
>
>

Well, I'm a FreeTDS developer. It seems that FreeTDS libraries do not
set character conversion informations sending parameters so core libTDS
do not convert but just send bytes as they are. This problem reside in
both 0.62 and 0.63 (developement) trunks. I'll fix ASAP. In the meantime
what FreeTDS version are you using?

freddy77



Relevant Pages

  • DBD::ODBC and FreeTDS
    ... I'm doing some test to get 100% working with FreeTDS (sql server odbc ... Analyzing log (unixODBC and FreeTDS one) it appear that binding is wrong ...
    (perl.dbi.users)
  • Re: MS SQL Server driver
    ... FreeTDS is a free native Linux library that ... >> connects to remote MS SQL servers just fine. ... Eventually, FreeTDS calls the server ... I use FreeTDS to talk from CMUCL to SQL server. ...
    (comp.lang.lisp)
  • Re: [opensuse] Adding php-mssql shared object to opensuse 10.3 to use freetds
    ... PHP uses ODBC through unixODBC, unixODBC then uses the FreeTDS ODBC ... drivers to connect to SQL. ... Does the pc sql server have to be operated in some sort of compatibility ...
    (SuSE)
  • Re: freetds - how to fix ?
    ... I have to use data from M$ SQL server in one of PHP apps running ... The problem is that buggy freetds from sarge crashes very often. ... Or maybe there is some other way to access MS SQL server from PHP? ...
    (Debian-User)