I'm using the MySQL C API to read data from a UTF-8 database, created thus:

  CHARACTER SET utf8 COLLATE utf8_general_ci;

I was expecting to have to call mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"utf8") to read the UTF-8 data, but found that it seems to assume that the data was ISO-8559-1 and apply an ISO-8559-1 -> UTF-8 conversion to what was UTF-8 in the first place.

If I comment out the call to mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"utf8"), it works fine. I get my UTF-8.

That implies that setting the MYSQL_SET_CHARSET_NAME option only works for MySQL databases with the default encoding?!

Is that right?
const char *charset = "utf8";
/*if (mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,charset) != 0)
	cerr << "Warning: Unable to set CHARSET '" << charset << "' " << mysql_errno(&mysql) << ": " << mysql_error(&mysql) << endl;*/
if ((con = mysql_real_connect(&mysql,host,user,password,db,0,0,0)) == 0) {
	cerr << "Error: Unable to connect code " << mysql_errno(&mysql) << ": " << mysql_error(&mysql) << endl;

Open in new window

LVL 17
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

trinitrotolueneDirector - Software EngineeringCommented:
some of the options work only for a specific library. Are you linking to the libmysql library??
trinitrotolueneDirector - Software EngineeringCommented:
Besides there is an option which sets the location for the character set directory.


Logically this should precede the setting of an option for a default character set.

Have you set this ?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rstaveleyAuthor Commented:
Hi TNT, thanks for picking this up. This is being built and executed on a Linux Debian 4.0 (Etch) installation, linking with libmysqlclient.

i.e. my Makefile has

   LFLAGS = -L /usr/lib/mysql -lmysqlclient

The MySQL client version is "Ver 14.12 Distrib 5.0.32, for pc-linux-gnu (i486) using readline 5.2", which I guess marries up with the library version.

Are you saying that I need to invoke MYSQL_SET_CHARSET_DIR first?

If so, I see no mention of character-sets-dir in my.cnf, so I wonder where the default directory is for "The pathname to the directory that contains character set definition files"?

I saw no indication from the library that MYSQL_SET_CHARSET_NAME was failing.
// Do you mean...

Open in new window

rstaveleyAuthor Commented:
rstaveleyAuthor Commented:
It would be good to know where those character sets are, but thanks for pointing me anyhow, TNT.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.