开发者

Read utf-8 characters from MySql into C variables

I have a mysql database, with two fields (firstname, lastname) of type CHAR(255) and characterset utf_general_ci

Using mysqlphpadmin I set firstname to "Sàm" and lastname to "Èkméïß" In my c code I retrieve these fields using:

sscanf(row[SDB_INV_COL_FIRSTNAME],"%[^\n]",db_firstname);
sscanf(row[SDB_INV_COL_LASTNAME],"%[^\n]",db_lastname);

But when I do so the db_firstname variable is set to 'S\340m' and db_lastname is set to '\310km\351\357\227'

It seems like the characters are being switched to unicode or some other characterset, and I can't figure out how to correct this! How do I get my fields out as utf8 into my C variables?

My connection is setup like this (notice the charset setting):

conn = mysql_init(NULL);
if(conn == NULL) {
sprintf(tempstr,"Survey database connection init error: [%u] %s", mysql_errno(conn), mysql_error(conn));
logmessage(LLEVEL_ERROR, INTERROR_MYSQLCONNECT_FAILED, tempstr);
sdberrorstate = TRUE;
return FALSE;
}

if(!mysql_real_connect(conn, program_options.sdbhost , program_options.sdbusername , program_options.sdbpassword, program_options.sdbdatabase, program_options.sdbport, NULL, 0)) {
sprintf(tempstr,"Survey database real connection error [%u] [%s]", mysql_errno(conn), mysql_error(conn));
logmessage(LLEVEL_ERROR, INTERROR_MYSQLCONNECT_FAILED, tempstr);
s开发者_开发百科dberrorstate = TRUE;
return FALSE;
}

if(mysql_options(conn, MYSQL_SET_CHARSET_NAME, "utf8") != 0) {
sprintf(tempstr,"Survey database set characterset utf8 error [%u] [%s]", mysql_errno(conn), mysql_error(conn));
logmessage(LLEVEL_ERROR, INTERROR_MYSQLCONNECT_FAILED, tempstr);
sdberrorstate = TRUE;
return FALSE;
}


You are receiving the data in ISO-8859-1. MySQL has encoding settings not only for the database tables, you should also set correctly the encoding to use for the MySQL client.

Edit:

From mysql_options() docs:

mysql_options() should be called after mysql_init() and before mysql_connect() or mysql_real_connect()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜