1.首先pb需要utf8格式,所以由ansi转换成utf8格式才能传输
2.在接收端,需要将utf8转换成ansi才能用
3.在插入数据库之前需要将sql从ansi转换成utf8格式
4.数据库设置字符集为utf8 ASSERT( 0 == Query(&m_con, "SET NAMES utf8", strlen("SET NAMES utf8")));
db_user_mail* pMail = offline->mutable_user_mail(); ASSERT(pMail->has_content());//系统邮件肯定需要content char* pContent = UTF8ToANSI(pMail->content().c_str()); char sql[1024 * 10] = {0}; sprintf_s(sql, sizeof(sql), "INSERT INTO `user_sys_mail` SET `content` = ‘%s‘, `type` = %d, `send_time` = from_unixtime(%u), `valid_time` = %u;SELECT LAST_INSERT_ID();", pContent, pMail->type(), pMail->send_time(), offline->valid_time()); free(pContent); char* pSql = ANSIToUTF8(sql); int code = CDBMgr::Query(con, pSql, strlen(pSql)); free(pSql); if (0 != code) //非0查询失败 { printf("query failed! [%s] [%d] [%s] \n", sql, mysql_errno(con), mysql_error(con)); return -1; }
插入中文的格式,当时这个问题给我带来了巨大的困扰,吗的都是坑。。
时间: 2024-11-06 03:45:16