gpt4 book ai didi

mysql - 从MySql读取utf-8字符到C变量中

转载 作者:行者123 更新时间:2023-11-30 18:04:25 24 4
gpt4 key购买 nike

我有一个 mysql 数据库,有两个类型为 CHAR(255) 的字段(名字、姓氏)和字符集 utf_general_ci

使用 mysqlphpadmin 我将名字设置为“Sàm”,姓氏设置为“Èkméïß”在我的 C 代码中,我使用以下方法检索这些字段:

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

但是当我这样做时,db_firstname 变量设置为“S\340m”并且 db_lastname 设置为 '\310km\351\357\227'

字符似乎正在切换为 unicode 或其他字符集,我不知道如何纠正这个问题!如何将我的字段以 utf8 格式输出到我的 C 变量中?

我的连接设置如下(注意字符集设置):

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);
sdberrorstate = 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;
}

最佳答案

您正在接收 ISO-8859-1 格式的数据。 MySQL 不仅有数据库表的编码设置,您还应该正确设置 MySQL 客户端使用的编码。

编辑:

来自mysql_options() docs:

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

关于mysql - 从MySql读取utf-8字符到C变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7639130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com