I was not able to replicate your results. I loaded utf8 text data from a file into a table and got it back unharmed, using both the mysql command line utility and a perl DBI script.

The one difference between my Perl/DBI attempt and yours might be that I prepared the insert (or update) statement with a placeholder where the text value would go, rather than trying to build the statement with the actual text value in place and trying to quote it somehow -- that is:

my $db = DBI->connect( blah, blah ); my $sql = $db->prepare( "insert into my_table (col1,col2) values (?,?) +" ); while (<>) { # assume we are reading rows of data... chomp; my ( $v1, $v2 ) = split( /\t/ ); # ... tab-delimited $sql->execute( $v1, $v2 ); } $sql->finish; # now try reading stuff back: $sql = $db->prepare( "select col1,col2 from my_table" ); $sql->execute; # (update: forgot to put this in at first) my $rowref = $sql->fetchall_arrayref; for my $row ( @$rowref ) { print join( "\t", @$row ), $/; } $sql->finish;
As a strange little aside: you may need to be careful in your perl script about setting the character semantics on whatever file handles you use for input and output. If both are supposed to be utf8, then I suggest being explicit about that in your perl code (e.g. include  binmode(STDOUT,":utf8"); if you're writing stuff to STDOUT).

As for controlling character semantics in the database transactions, in general I'd say don't -- data is data as far as the RDBMS is concerned (mysql or other), and whatever byte sequence you put in, that's what you'll get back, so long as you use DBI's placeholder / parameter syntax for putting data values into the sql statements.


In reply to Re: Perl Mysql and UTF8 by graff
in thread Perl Mysql and UTF8 by emilioayllon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.