bobi123bobi
bobi123bobi
采纳率16.7%
2019-05-02 13:15

c++向mysql插入string类数据出错

已采纳

报错:插入 is failure ,error is :Incorrect string value: '\xF0\xC2!\xED2\x01' for column 'sname' at row 1

代码:#include
#include
#include
#include
#include"windows.h"
#include
#include

using namespace std;

int main(){

MYSQL mysql;
mysql_init(&mysql);
int t = 100;
string a = "ticke";
if (!mysql_real_connect(&mysql, "localhost", "root", "hsy1019", "studentmanage", 3306, NULL, 0))
{
    cout << "\n数据库连接发生错误";
    return 0;
}
else
    cout << "connect successfully";
int i = 0;
//int res;
char query[100];
cout << "学生信息为       学号         姓名" << "\n";

    sprintf_s(query, "insert into studentg(sno,sname) values('%d','%s')", t,a);
    if (mysql_query(&mysql, query))
        fprintf(stderr, "插入 is failure ,error is :%s\n", mysql_error(&mysql));
    else
        puts("插入 success!");
    //res = mysql_query(&mysql, "insert into studentg(sno,sname) values(stu[i].id,stu[i].name");


mysql_close(&mysql);
system("pause");
return 1;

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • oZhuXian 铸仙 2年前

    这不是数据库的错误,字符串string 不是用%s, 你可以输出query 看看
    sprintf_s(query, "insert into studentg(sno,sname) values('%d','%s')", t,a);
    改成
    sprintf_s(query, "insert into studentg(sno,sname) values('%d','%s')", t,a.c_str());

    点赞 评论 复制链接分享

为你推荐