MySql中插入汉字

ydchou 2007-12-19 01:13:25
小弟最近使用VC+MySql

在命令行下输入:insert into table values(10,'您好',5);
正常
但在VC下使用代码输入:
res = mysql_query(&my_connection, "insert into table values(20,'失败',5)");
出错,出错提示为:Insert error 1406: Data too long for column 'fname' at row 1

请问各位大虾如何使用VC在数据库中插入汉字!
...全文
110 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ydchou 2007-12-20
  • 打赏
  • 举报
回复
还是不行啊!
显示还是乱码!
代码是这样的:
///////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <mysql.h>
#include <string.h>

main()
{
MYSQL my_connection;

int res;

mysql_init(&my_connection);

if (mysql_real_connect(&my_connection, "localhost", "root", "mypasswd","myDB",0,NULL,CLIENT_FOUND_ROWS))
{
printf("Connection success\n");
res = mysql_query(&my_connection, "insert into test1 values(10,'中',5)");

if (!res)
{
printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));
//里头的函数返回受表中影响的行数
}
else
{
//分别打印出错误代码及详细信息
fprintf(stderr, "Insert error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
}
mysql_close(&my_connection);
}

else
{
fprintf(stderr, "Connection failed\n");

if (mysql_errno(&my_connection))
{
fprintf(stderr, "Connection error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
/////////////////////////////////////////////
创建的表:create table test1(no int not null unique,name varchar(20),age int);
所有编码也设成utf8了,可还是不行啊!!
XFliangwh 2007-12-20
  • 打赏
  • 举报
回复
登陆时用mysql --default-character-set=utf8 -u root -p

其实数据库已经插入了汉字,只不过是环境问题,使得显示乱码
ydchou 2007-12-19
  • 打赏
  • 举报
回复
谢谢!
不过我试了,能插进去汉字,但是现实是乱码!
改成GB2312编码后,还是不行。
XFliangwh 2007-12-19
  • 打赏
  • 举报
回复
改my.ini
[mysql]
default-character-set=utf8
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8

建立表时
CREATE TABLE `XX`
(
.....................
)ENGINE=InnoDB DEFAULT CHARSET=UTF8;
DEFAULT CHARSET=UTF8这个是关键

用以上方法试试啦.

56,678

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧