In my C++ application (On Windows x64 being developed using VS2012) I am using MySQL Connector/C++ to connect MySQL database. I've table ALL_USERNAMES that has a column:
USERNAME VARCHAR(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci
However, I am not sure how can I fire utf8 queries through my C++ application. Method sql::Statement::executeUpdate
accepts sql::SQLString
type. It is essentially std::string
.
So if I do something like this:
std::string strQuery = "INSERT INTO ALL_USERNAMES (USERNAME) VALUES ('अतुल')"
executeUpdate(strQuery);
And when I check the table record through MySQL Workbench I see USERNAME as all question marks (???????).
I couldn't find anything about this in Connector/C++ documentation. Any idea how to solve this?
What I wanted to try:
In MySQL C API there are functions like:
mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8");
mysql_options(&mysql, MYSQL_INIT_COMMAND, "SET NAMES utf8");
which I wanted to try as I suspect it will solve my problem. But I couldn't find its counterpart in Connector/C++.
UPDATE:
SHOW CREATE TABLE
gives me this:
'ALL_USERNAMES', 'CREATE TABLE `all_usernames` (\n `USERNAME` varchar(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8'
And my client code is here (this was before I written my own answer):
sql::Connection *m_pConnection = NULL;
sql::Statement *m_pStatement = NULL;
sql::Driver *driver = NULL;
driver = get_driver_instance();
m_pConnection = driver->connect("tcp://127.0.0.1:3306", "username", "password");
m_pConnection->setSchema("DBNAME");
m_pStatement = m_pConnection->createStatement();
int RetVal = m_pStatement->executeUpdate("INSERT INTO ALL_USERNAMES (USERNAME) VALUES ('अतुल')");