Link to home
Start Free TrialLog in
Avatar of rstaveley
rstaveleyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Misusing MYSQL_SET_CHARSET_NAME

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

CREATE DATABASE IF NOT EXISTS `amg`
  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?
mysql_init(&mysql);
 
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;
	exit(1);
}

Open in new window

Avatar of trinitrotoluene
trinitrotoluene
Flag of Australia image

some of the options work only for a specific library. Are you linking to the libmysql library??
ASKER CERTIFIED SOLUTION
Avatar of trinitrotoluene
trinitrotoluene
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rstaveley

ASKER

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...
 
mysql_options(&mysql,MYSQL_SET_CHARSET_DIR,"/somepath");
mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"utf8");

Open in new window

ping?
It would be good to know where those character sets are, but thanks for pointing me anyhow, TNT.