coeur2v has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I would like to upload a tab delimited file saved in unicode format in mysql. Could you give me some advice ?
Thanks for your help.
Regards

2001-08-20 Edit by Corion : Moved to Seekers, added formatting

Replies are listed 'Best First'.
Re: unicode file in mysql
by ichimunki (Priest) on Aug 21, 2001 at 01:09 UTC
    the following code is incomplete, I expect you will have to read a few perldocs for the CGI and DBI and DBD::mysql modules to fill it in and make sure everything is how it should be:
    #/usr/bin/perl -w use strict; use CGI; use DBI; #start our CGI object up my $Q = CGI->new(); #we probably want to set $CGI::POST_MAX, too. #initialize a DB handle my $dsn = 'username:host:password'; #see perldocs for exact form my $DBH = DBI->new( $dsn ); #check for a file my $file = $Q->param( 'uploaded_file' ); unless ( $file ) { #there is no file upload, so here's the form to do the upload print $Q->header(), $Q->start_html(), $Q->start_form(); print $Q->filefield( -name => 'uploaded_file' ); print $Q->end_html(); exit; } my $text = join( '', <$filename> ); #slurp the whole file up $DBH->do("INSERT INTO table VALUES( '$text' )"); #assumes blob or unic +ode text field in target table #status back to user print $Q->header(), $Q->start_hmtl, $Q->p( 'Done' ), $Q->end_html;

    Update: as to the unicode format issue, you can probably convert that stuff to HTML entities before you insert it, otherwise you might look at the blob field type (not ideal, but might work), after all, on some level it's just data.
      Thanks for your reply ichimunki, My major problem concerns the unicode format. I do not no how to convert it in standard text and html. I assume that I have to use the unicode module, but I am very short in time so any help is welcome.

        Actually Text::Iconv is your friend.

        #!/usr/bin/perl -w use strict; use Text::Iconv; use HTML::Entities; # create the conversion method (only once) my $conv = Text::Iconv->new("utf8", "latin1"); my $html_text = encode_entities( $conv->convert( $utf8_text));

        If you want to get fancy you can use the bit of code in converting character encodings

        I figured as much, you should be more specific in the question then! :)

        Unicode::String is the module I would look at. It appears to supply methods for converting unicode into a variety of more 8-bit friendly forms.
Re: unicode file in mysql
by Cine (Friar) on Aug 21, 2001 at 00:56 UTC
    As of right now there isnt suppert for unicode (as text) in mysql, do you want it stored in unicode or can it be converted to something like iso-8859-1?

    T I M T O W T D I
      Time, Thanks for yor reply.
      I would like to converter special signs (for instance greek symbols, trademark, etc..) in html and the "normal" characters in "normal" text
Re: unicode file in mysql
by Cine (Friar) on Aug 21, 2001 at 13:56 UTC