1
votes

Hi I am completely new to database. Here I am having problem inserting a row into a table. The string comes in is in Unicode and converted to UTF8 using WideCharToMultiByte call. Then constructed a database query as below.

in = "Weiß" (UTF8 string result of conversion from Unicode to UTF8)


wchar_t buf[2048]; //= new wchar_t[ in.size() ];
size_t num_chars = mbstowcs( buf, in.c_str(), in.size() );
wstring ws( buf, num_chars );

Here I have the string ws = 'WeiÃ' If I expand ws to see there are 5 characters in the string and the 5th character is 159:L''.

wostringstream oss;
oss << L"insert into myutftable values("
                                       << id
                                       << L"',  '"
                                       << ws
                                       << L") ";

Then I am using SQLExecDirect to update the database. This is the place I am seeing the crash.

I am trying to understand why it crashes. I have trying few things unsuccessfully. I used character set = utf8 but no luck. Can anyone tell me what could be the reason for crash and how to fix?

BTW I am using Firebird database version 2.0.

UPDATE1:

1) If I do not convert UTF8 string to wstring and use SQLExecDirectA call it works fine. But at this point I do not know side effects because that database has been accessing from lot other places.

2) I have tried pushing the same string from command line using isql.exe, no issues!

I wonder why in my debugger watch list the last character was not displayed (Ÿ this character after converting to wchar shown as 159:L''. I looked in character set 159 refers to Ÿ. Any ideas why my debugger does not show this character as wchar, but it was displayed as just char?

Is there a way to Debug SQLExecDirect? I am using OdbcJdbc Drivers.

Update 2: Seems like I am having issue with Ÿ character in my strings. As long as I am sending it in as string no issues if I use wstring at all it fails.

I just observed in memory how it was represented when I am sending it as 9f (= Ÿ) no issues with SQLExecDirectA if I send it as 9f 00 (= Ÿ wchar) then it crashes on SQLExecDirect

My application was built using Unicode Character Set. Firebird database character set set to none.

Any ideas??

1
I don't think delete [] buf would be wise considering you never newd it. Just... ouch. Count on your heap being hosed after that little gem of undefined behavior. pebkac. - WhozCraig
I agree, I do not have that call anymore in my code. - Vivek
With 'crash', do you mean a Firebird server crash, connection closed, or a crash in your application? - Mark Rotteveel
Crash in my application as soon as I call SQLExecuteDirect. It did not return anything. If I construct sql statement in std::string it works fine. But if I look into database that string is not displayed correctly. - Vivek

1 Answers

4
votes

mbstowcs assumes its second parameter to be a string in the system default code page, also known as CP_ACP, which is never UTF-8 (also known as CP_UTF8).

The inverse of WideCharToMultiByte is MultiByteToWideChar. Though it's unclear why you want to convert a string from Unicode to UTF-8, only to convert it right back.