[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Misusing MYSQL_SET_CHARSET_NAME

Posted on 2008-11-12
5
Medium Priority
?
3,251 Views
Last Modified: 2012-05-05
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
Comment
Question by:rstaveley
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 22947825
some of the options work only for a specific library. Are you linking to the libmysql library??
0
 
LVL 12

Accepted Solution

by:
trinitrotoluene earned 2000 total points
ID: 22947838
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
 
LVL 17

Author Comment

by:rstaveley
ID: 22948336
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
 
LVL 17

Author Comment

by:rstaveley
ID: 23001980
ping?
0
 
LVL 17

Author Closing Comment

by:rstaveley
ID: 31516279
It would be good to know where those character sets are, but thanks for pointing me anyhow, TNT.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question