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

Hi Everyone,

I am hoping someone can help me out here. I am having a problem getting some text into a MSSQL DB correctly using the following CGI script (i tried to trim it down to just the essentials).

use CGI; use DBI; my $test = 'testこんにちはtest'; print "Content-type: text/plain; charset=utf8\n\n"; my $dbproc = DBI->connect("DBI:ODBC:*********","*******", "*****", + { PrintError => 0, RaiseError => 1, ChopBlanks => 1 } ) || print "un +able to insert record $DBI::errstr"; my $insQry = "exec ins_result N'".$test;."'"; print "insQry: $insQry\n\n"; my $sth = $dbproc->prepare($insQry); $sth->execute; $dbproc->disconnect || print "unable to insert record $DBI::errstr +";
I see the correct data on the web page as follows: insQry: exec ins_result N'testこんにちはtest'

but when i check the DB i see the following was inserted into the table:

mikeSkaotest

Environment details are:

OS: Red Hat Enterprise Linux Client release 5.9 (Tikanga)

DB Microsoft SQL Server 2005

Apache: Apache/2.0.50

Perl: 5.10.0

Any help would be greatly appreciated.

Thanks,

Mike

Replies are listed 'Best First'.
Re: Help inserting Kanji in MSSQL DB
by CountZero (Bishop) on Dec 21, 2014 at 18:28 UTC
    The string you try to save into the database contains HTML-entities, not UNICODE characters.

    Try to decode the HTML-entities to UNICODE with the decode_entities function of HTML::Entities.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Help inserting Kanji in MSSQL DB
by Laurent_R (Canon) on Dec 21, 2014 at 18:04 UTC
    I do not know much about MS SQL Server, but possibly your database is not set up for storing UTF-8 strings.

    Update: Forget what I wrote above. I did not even see it, but, of course, CountZero is right, what you are trying to write to the DB is not Unicode code points or UTF-8 strings, but HTML.

      I just tried running the CGI script from the command line using "perl test.cgi" and it inserted the Kanji correctly so now I don't think it is Perl or DB related. Now I am thinking it may be something Apache is doing with the CGI when running the script. I will check on the Apache site. Thanks for the quick reply!