#include#include #include using namespace std; const char* host = "127.0.0.1"; const char* user = "root"; const char* pw = "123456"; const char* database_name = "database_test"; const int port = 3306; typedef struct Student { int student_id; string student_name; string class_id; }Student; int main() { MYSQL* con = mysql_init(NULL); //设置字符编码 mysql_options(con, MYSQL_SET_CHARSET_NAME, "GBK"); if (!mysql_real_connect(con, host, user, pw, database_name, port, NULL, 0)) { fprintf(stderr, "Failed to connect to database : Error:%s\n", mysql_error(con)); return -1; } Student stu{ 1111, "吴彦祖", "软件一班" }; char sql[1024]; //sprintf(sql, "insert into students (student_id,student_name,class_id) values(%d,'%s','%s')", // stu.student_id, stu.student_name.c_str(), stu.class_id.c_str()); sprintf_s(sql, sizeof(sql), "insert into students (student_id,student_name,class_id) values(%d,'%s','%s')", stu.student_id, stu.student_name.c_str(), stu.class_id.c_str()); if (mysql_query(con, sql)) { fprintf(stderr, "Failed to insert data : Error:%s\n", mysql_error(con)); return -1; } mysql_close(con); system("pause"); return 0; }