• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3402
  • Last Modified:

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

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

character-sets-dir=path

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

Have you set this ?
0
 
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...
 
mysql_options(&mysql,MYSQL_SET_CHARSET_DIR,"/somepath");
mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"utf8");

Open in new window

0
 
rstaveleyAuthor Commented:
ping?
0
 
rstaveleyAuthor Commented:
It would be good to know where those character sets are, but thanks for pointing me anyhow, TNT.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now