定义数据库操作类
class Mysql {
public:
/***************************连接数据库***************************/
MYSQL *mysqlConnect(conn_info con) {
MYSQL *mysql = mysql_init(NULL); //mysql初始化函数,实例化一个新的对象(NULL为新的对象,否则为初始化以创建的对象)
if (!mysql_real_connect(mysql, con.host, con.user, con.password, con.database, 3306, NULL, 0)) { //mysql_real_connect(MYSQL *mysql,const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag)
cout << "connection error";
exit(1);
}
cout << "connection success" << endl;
return mysql; //返回实例化的对象
}
/**************************数据库操作函数**************************/
void mysqlOperation(MYSQL *mysql, char *sql_op) {
if (mysql_query(mysql, sql_op)) { //mysql_query()函数为对mysql进行操作,第一个参数为刚才实例化的对象,第二个位需要进行的操作
printf("MySQL query error : %s", mysql_error(mysql));
getchar();
exit(1);
}
}
/*****************************修改数据****************************/
void updateData(MYSQL *mysql, const char *tableNames, const char *col, int data, int id) {
char cmd[100];
char data2[5];
char id2[5];
const char *data2s = itoa(data, data2, 10);
const char *id2s = itoa(id, id2, 10);
strcpy(cmd, "update ");
strcat(cmd, tableNames);
strcat(cmd, " set ");
strcat(cmd, col);
strcat(cmd, "=");
strcat(cmd, data2s);
strcat(cmd, " where id");
strcat(cmd, "=");
strcat(cmd, id2s);
strcat(cmd, "");
mysqlOperation(mysql, cmd);
}
/*****************************查询函数****************************/
MYSQL_RES *mysqlSelect(MYSQL *mysql, const char *table, MYSQL_RES *res) {
char cmd[100];
strcpy(cmd, "select * from ");
strcat(cmd, table); //拼接指令
mysql_free_result(res);
mysqlOperation(mysql, cmd);
return mysql_store_result(mysql);//查询的结果,用mysql_store_result函数后接mysql_affected_rows()可以返回查询次数,而用mysql_use_result()则不行
}
/****************************显示所有数据***************************/
void mysqlShow(MYSQL_RES *res, vector<string> &colName) {
MYSQL_ROW col;
while ((col = mysql_fetch_row(res)) != NULL) // row = mysql_fetch_row(res);进行数据的转换,需要循环转换
{
for (int i =0; i < colName.size(); i++)
cout << col[i] << " "; //显示的行数,注意此处表格中只有两列,故row也只有2列,若写多列会出错
cout << endl;
}
}
创建表格
//创建表格
class TableOperation: public Mysql {
public:
MYSQL *connectTbale() {
MYSQL *conn;//用于实例化的新的对象
struct conn_info info;
Mysql mysql;
info.host = "localhost";
info.user = "root";
info.password = "111111";
info.database = "gy1"; //我的数据库名字
conn = mysql.mysqlConnect(info); //连接数据库
return conn;
}
void updateAllData(MYSQL *mysql, const char *tableName, const char *colName,vector<int> data, vector<string> &col) {
for (int i = 0; i < data.size(); i++) {
updateData(mysql, tableName, colName, data[i], i + 1);
}
}
void showAllData(MYSQL *mysql, const char *tableName, vector<string> &colName) {
MYSQL_RES *res;
res = NULL;//要先初始化,否则会报错
for (vector<string>::const_iterator iter = colName.begin(); iter != colName.end(); ++iter)
cout << *iter << " ";
cout << endl;
res = mysqlSelect(mysql, tableName, res);
mysqlShow(res, colName);
}
};
测试程序
// id name gender score
// 1 std1 1 60
// 2 std2 0 70
// 3 std3 1 80
struct conn_info {
char *host;
char *user;
char *password;
char *database;
};
int main() {
string colName[4] = { "id", "name", "gender", "score" };
int scoreData[4] = { 55, 65, 75, 85 };
int genderDate[4] = { 0, 1, 0, 0 };
vector<string> colname(colName,colName+4);
vector<int> scoredata(scoreData, scoreData + 4);
vector<int> genderdata(genderDate, genderDate + 4);
TableOperation tableOperation;
MYSQL *conn;
conn = tableOperation.connectTbale();
tableOperation.updateAllData(conn, "student", "score", scoredata, colname);
tableOperation.updateAllData(conn, "student", "gender", genderdata, colname);
tableOperation.showAllData(conn, "student", colname);
system("pause");
mysql_close(conn);//断开连接
return 0;
}
本程序仅供参考,如有错误请见谅。