Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting UTF-8 data from MySQL to the Linux C++ application

I have a big troubles with display of UTF-8 data retrieved from the MySQL to the Linux-based C++ application. UTF text is shown as question marks.

The application uses the MySQL C API. So I passed the UTF-8 option after mysql_init() and before mysql_real_connect():

mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, 'utf8');

and

mysql_options(&mysql,MYSQL_INIT_COMMAND, 'SET NAMES utf8');

But without luck. The test is still displayed as question marks. I made a few tests with a Perl script (I more familiar with it ;) ). And the text is displayed correctly if I specify the UTF-8 option for the connection:

$dbh->{'mysql_enable_utf8'} = 1;
$dbh->do('SET NAMES utf8');

Any idea how to display UTF-8 data in the C++ application correctly?

like image 452
michael Avatar asked Dec 31 '25 19:12

michael


1 Answers

It could be a simple typo. You write:

mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, 'utf8');

The single quotes are for specifying character literals, not strings. So, change that to:

mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8");

Also, check the type of mysql. If it is MYSQL *, then write:

mysql_options(mysql, MYSQL_SET_CHARSET_NAME, "utf8");

The same applies to the line with MYSQL_INIT_COMMAND.

like image 149
feklee Avatar answered Jan 02 '26 11:01

feklee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!