perl-unicode

RE: UTF-16 -> UTF-8

2001-11-21 09:33:19
Philip,

I've rewritten the database write code to use a Command Object, because its the 
proper way to do appends. Although now that I look
at the previous code I'm completely surprised how it even worked ocasionally, 
the surprinsing conclusion is that it did work,
because the results are no different. The BD still doesn't properly recognize 
the Unicode chars.

Just for reference, here goes the code:

$conn = Win32::OLE->new('ADODB.Connection') || die ("Bolas, que ja morri..");
$conn->open($constr);
$cmd = Win32::OLE->new('ADODB.Command') || die ("Bolas, que ja morri..");



while (<FICH1>) {
        chomp($_);
        $palavra_utf8= utf8($_);
        $palavra_utf8->byteswap;
        $palavra_utf16=$palavra_utf8->utf16;
        $sql="INSERT INTO Tipo_Referencia ( Descricao ) VALUES 
('$palavra_utf16');";
        $cmd->{"ActiveConnection"} = $conn;
        $cmd->{"CommandText"} = $sql;
        $cmd->{"CommandType"} = adCmdText;
        print FICH3 $palavra_utf16;
        $cmd->execute;
}

Don't lose more time over this. It seems there is some kind of problem with the 
recognition of the encoding from other Office apps.
Its rather surprising that Notepad regosnizes the characters properly and Word 
and Access don't.

I really appreciate the time and patience you took to analise what we've been 
doing.

Thank you.  Best regards.

Rui

-----Original Message-----
From: Philip Newton [mailto:Philip(_dot_)Newton(_at_)gmx(_dot_)net]
Sent: quarta-feira, 21 de Novembro de 2001 15:45
To: Rui Ribeiro
Cc: perl-unicode(_at_)perl(_dot_)org
Subject: Re: UTF-16 -> UTF-8


On Wed, 21 Nov 2001 15:14:38 -0000, in perl.unicode you wrote:

Still can't write to the BD though. The append SQL instruction has no 
effect.

It looks wrong to me, too.

use Unicode::String qw(utf8 latin1);

You don't need to import 'latin1' if you're not going to use it. (It's
not going to hurt, but it's not needed, either.)

open(FICH1,"fich1.txt")||die"Nao foi possivel abrir o ficheiro fich1.txt 
$!";
open(FICH3,">fich3.txt")||die"Nao foi possivel abrir o ficheiro fich3.txt 
$!";

Ah good, you've got $! in there, now.

$conn->open($constr);

Shouldn't you check whether your DB open succeeded?

    $sql =  "INSERT INTO Tipo_Referencia ( Descricao ) SELECT VALUES 
('$palavra_utf16');";

I think the 'SELECT' should not be there. Just 'INSERT INTO table
(columns) VALUES (literals)'. (And with DBI I leave off the semicolon at
the end; I don't know what ADODB.Connection wants.)

    print FICH3 $palavra_utf16;
    $conn->execute($sql,,,adExecuteNoRecords);

Why don't you write

    $conn->execute($sql, adExecuteNoRecords);

? The two extra commas don't change the fact that you're only passing
two arguments to execute.

Does this function give you a way to check the result? Again, I don't
know ADODB.Connection. But maybe the database itself can tell you what
went wrong? SQL syntax error, database connection not there, wrong
password, whatever....

close(FICH3);

You should also check whether the close succeeded, at least on files
that you opened for writing. For example, if you get a disk full error,
then the close will fail and you can examine $! then. (You could also
check the return value of every print() you do, in order to catch this
error earlier, but in practice nobody does that. But you should still
check your close.)

Cheers,
Philip


<Prev in Thread] Current Thread [Next in Thread>