List:MySQL ODBC« Previous MessageNext Message »
From:Shiwei Yin [Wesley] Date:September 9 2009 9:32am
Subject:MySQL ODBC Driver Unicode/UTF-8 Support Issue
View as plain text  
Hi,
Now I use Connector/ODBC 5.1 driver, the exact driver information are as follows:
Driver = myodbc5.dll
Driver Version = 05.01.0005
And MySQL database version is: 5.1.36-community MySQL Community Server

I use VC++ to connect to MySQL database. I want to insert a Chinese character into a
table. Here is the table definition from "show create table cs_test2" command:
CREATE TABLE `cs_test2` (
  `val` varchar(20) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1

The following are the code to insert value into this table.
/////////////////////////////////////////////////////////////////////////
char* StrIns = "insert into cs_test2 values (?)";
unsigned char ValBuf[5] = {0xE6,0xAF,0x8F,'\0','\0'};
SQLRETURN ret;
SQLINTEGER cbValue = 3;
SQLLEN RowCount;
ret = SQLPrepare(hstmt,(SQLCHAR*)StrIns,strlen(StrIns));
if (ret == SQL_SUCCESS)
{
ret=SQLBindParameter(hstmt,1,SQL_PARAM_INPUT,SQL_C_WCHAR,SQL_WVARCHAR,20,0,ValBuf,0,&cbValue);
                if (ret == SQL_SUCCESS)
                {
                        ret = SQLExecute(hstmt);
                        if (ret == SQL_SUCCESS)
                        {
                                ret = SQLRowCount(hstmt,&RowCount);
                                if (ret == SQL_SUCCESS)
                                {
                                        printf("Affected %d row.\n", RowCount);
                                }
                        }
                        SQLFreeStmt(hstmt,SQL_CLOSE);
                }
}
////////////////////////////////////////////////////////////////////////////
In the code, I use SQL_C_WCHAR and SQL_WVARCHAR as the C data type and SQL data type
respectively.

The code can be executed successfully. However, when I query the database using "select
hex(val) from cs_test2" in MySQL Query Browser, the inserted character hex value is
'EABFA6'. But in my code, the value I inserted should be 'E6AF8F'.

Why the value is changed during the insertion? What is the problem? Is it a driver issue?

Thanks,
Wesley
Thread
MySQL ODBC Driver Unicode/UTF-8 Support IssueShiwei Yin [Wesley]9 Sep