C/C++連接Mysql數(shù)據(jù)庫 | 數(shù)據(jù)庫增刪改查C++封裝 | 信息管理系統(tǒng)通

報錯Failed to connect to database : Error:This handle is already connected. Use a separate handle for each connection.
解決了。將代碼修改成這樣就不會報這個錯。#include <mysql.h>
#include <iostream>
#include <string>
using namespace std;
const char* host = "localhost";
const char* user = "root";
const char* pw = "123456";
const char* database_name = "database_test";
const int port = 3306;
typedef struct People
{
??int people_id;
??string?people_name;
??string?address_id;
} People;
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;
??}
??People stu{ 1111,"吳彥祖","軟件工程2班" };
??char sql[1024];
??sprintf(sql, "insert into people (people_id,people_name,address_id) values(%d,'%s','%s')",
????stu.people_id, stu.people_name.c_str(), stu.address_id.c_str());
??if (mysql_query(con, sql))
??{
????fprintf(stderr, "Failed to insert data : Error:%s\n", mysql_error(con));
????mysql_close(con);
????return -1;
??}
??mysql_close(con);
??return 0;
}