zod has asked for the wisdom of the Perl Monks concerning the following question:
When inserting unicode text (chinese in my case) into MySQL, I set up the connection like so:
However, I haven't been able to replicate this on MS SQL Server 2008.my $dbh = DBI->connect( 'DBI:mysql:db_main', 'user', 'pass', { RaiseError => 1, AutoCommit => 1, mysql_enable_utf8 => 1, on_connect_do => [ "SET NAMES 'utf8'", "SET CHARACTER SET +'utf8'" ] } )
My connection looks like this:
That allows me to connect and select from tables, etc., but when I try to insert Chinese text, it inserts garbage. So, how do you specify utf-8 for the ODBC driver? Any ideas?my $dbh = DBI->connect('DBI:ODBC:Driver={SQL Server};Server=SQL2008;Da +tabase=db_main;UID=user;PWD=pass') or die "$DBI::errstr\n";
Thanks
UPDATE: I resolved this. I'll spare you the gory details involved with my particular environment (chinese version of windows on one box, not another, collations, etc.), but the key turns out to be this from the SQL Server BOL:
"When referencing the Unicode character data types nchar, nvarchar, and ntext, 'expression' should be prefixed with the capital letter 'N'. If 'N' is not specified, SQL Server converts the string to the code page that corresponds to the default collation of the database or column. Any characters not found in this code page are lost."
I did not have the "N" on my insert statement in perl. So, this worked with the 'N':
my $sql = "insert test values (N'中文')";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBI, unicode, ms sql server
by graff (Chancellor) on Feb 08, 2009 at 04:01 UTC | |
|
Re: DBI, unicode, ms sql server
by Anonymous Monk on Feb 08, 2009 at 03:14 UTC | |
by zod (Scribe) on Feb 08, 2009 at 03:38 UTC | |
by Anonymous Monk on Feb 08, 2009 at 11:46 UTC | |
by zod (Scribe) on Feb 08, 2009 at 16:58 UTC | |
by Anonymous Monk on Feb 08, 2009 at 04:05 UTC | |
by zod (Scribe) on Feb 08, 2009 at 04:12 UTC |