Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DBI using DBD::ODBC on MS SQL 2008 R2 corrupting Unicode characters

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I have found that using DBI with the DBD::ODBC driver for Windows is corrupting Unicode characters in nvarchar defined columns.

I did a test using Win32::ODBC and I don't get the Unicode corruption.

Is there a switch or flag or parameter I need to use when utilising DBI on Windows to stop this Unicode corruption?

I use the following to access the DB (not including the connection!)

Code:
    # Set Long Read (required for DBI & ODBC!)
    $self->dbh->{LongReadLen} = 99999; 
    
    # Run SQL Command
    my $sth = $self->dbh->prepare($sql) || die('Error in getSQL (' . $self->server . "): $sql");

    $sth->execute();
   
    # Declare array
    my @rs;
    
    # Loop SQL & build recordset
    while (my $ref = $sth->fetchrow_hashref()) {
        # Build Array of Hashes with SQL Data
        $rs[@rs] = \%$ref;	
    }  
        
    # Close DB Connection
    $sth->finish();

    # Disconnect from the database.
    $self->dbh->disconnect();
            
    # Return record set
    return @rs;

Any Unicode character retrieved seems to get corrupted into an unrecognised 'wide character'.

If I use the same logic but with Win32::ODBC module...
Code:
# Run SQL Command
if(!$db->Sql("$sel")) {

	# Loop SQL Record Set
		while($db->FetchRow()){ 
				# Build Array of Hashes with SQL Data
				my %dt = $db->DataHash();
			    $rs[@rs] = \%dt;	
		}
	# Close DB Connection
	$db->Close();

	# Return Record Set Array of Hashes
	@rs;

} 
else{die "Error in getSQL ($sel)" . Win32::ODBC::Error();}
...the Unicode chars are fine?

I know I could simply go back to Win32::ODBC, but this is being run with Catalyst, and the community prefer DBI, so was hoping to find a resolve with DBI, especially as I intend at some point to move to the DBIC ORM, which won't be an option if DBI on Windows can't handle Unicode characters properly.

Your help is appreciated.

Regards,
1DMF

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top