Piping into MySQL as UTF-8
Want a quick, lazy, command line way to pipe iso-8859-1 ( or any character encoding ) SQL into MySQL as UTF-8? Here's a little four-liner for you!
-
MySQL Logo
Scenario:
You send an export of your content database to be translated into French. When it comes back you want to pipe it back into MySQL but the text you copy out of Excel's not UTF-8.
We often handle foreign language data given to us as Excel spreadsheets and use CONCATENATE() to build up the SQL to pour the data into MySQL... it's not pretty but it's pretty quick and flexible. We use the following four lines in our pipe to make sure the data's UTF-8 when it goes in.
#!/usr/local/bin/perl -w
use strict;
use Encode qw( from_to is_utf8 );
while (<>){
from_to( $_, "iso-8859-1", "utf8");
print;
Usage:
All you need to do is save that somewhere sensible and add it into your pipe:
#cat foo*.sql | ~/to-utf8.pl | mysql -p database
Question:
Anyone know why some French hyphens and apostropies are still difficult?
1 Comments on this post
Speak to an Expert now on 01225 444 674
Talk to Us
Simon said “Cool ...” a few months ago