小弟最近使用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在数据库中插入汉字!
在命令行下输入: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在数据库中插入汉字!
[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这个是关键用以上方法试试啦.
显示还是乱码!
代码是这样的:
///////////////////////////////////
#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了,可还是不行啊!!