Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HI All,

I am trying to sending a unicode data e.g. 日本語 using Mysql source table to Mysql destination table by C++ code but 日本語 is replaced by ??? words,i tried with connection string utf-8 but did't work.

any solution..
Posted
Updated 8-Dec-11 18:46pm
v3
Comments
JackDingler 9-Dec-11 10:15am    
You asked a similar question later.

Perhaps if you gave more information about what you're specifically trying to do, and what technology you're leveraging we might be able to help.

I can't tell if you're have trouble connecting, sending data, how you're connecting to MySQL ...,
AMIT KUAMR SINGH 14-Dec-11 8:03am    
Yes I was connecting in MYSQL.I got the solution
1) in connection String i have set charset=utf8;
EX : bsConnectionString = "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=mydatabase;Uid=root;Pwd=amit;charset=utf8;pooling=false;";
2) data value what i was geting as wstring 1st i converted into utf8 string then again to wstring so things are working fine
EX:
// For MYSQL DataBase wString Need to convert In UTF8
if((m_pAdo->GetProviderType()== MYSQL)&&(varValue.vt == VT_BSTR))
{
wsValue = strtowstr(ToUtf8(wsValue));
}
td::string ToUtf8(const std::wstring& widestring)
{
int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);
if ( utf8size == 0)
{
throw std::exception("Error in conversion.");
}

std::vector<char> resultstring(utf8size);

int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);
if (convresult != utf8size)
{
throw std::exception("La falla!");
}

return std::string(&resultstring[0]);
}


std::wstring strtowstr(const std::string &str)
{ // Convert an ASCII string to a Unicode String
std::wstring wstrTo;
wchar_t *wszTo = new wchar_t[str.length() + 1];
wszTo[str.size()] = L'\0';
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wszTo, (int)str.length());
wstrTo = wszTo;
delete[] wszTo;
return wstrTo;
}

1 solution

UNICODE is 16 bit, and probably as little endian(or check for endianess).

if i am not wrong you receive them as byte stream. Transfer them wchar_t type. but I am not sure if this is the problem.

Another thing If you store data as UTF-8 only then you can read as UTF-8. If you want to transfer UTF-8 to UTF-16 then use mbstowcs function
before you start retrieving data you should select the encoding type:
SQL
SET NAMES 'charset_name' [COLLATE 'collation_name'];


For more information please read the following link[^]
 
Share this answer
 
Comments
AMIT KUAMR SINGH 15-Dec-11 1:31am    
Hi Johny Txs for ur sujestion.
i hv solves the problem by two steps
1)in connection String i have set charset=utf8;
2)data value what i was geting as wstring , i converted into utf8 string
then i am able 2 write it in correct formate.

now i am facing a problem while reading from My sql and writting it in ms access any solution u hv
Mohibur Rashid 15-Dec-11 1:51am    
MS Access probably save as UNICODE, try to save as unicode ie UTF-16

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900