VS2013 使用MySQL API连接数据库

环境配置

1.项目->属性->VC++目录,

添加包含目录:C:Program FilesMySQLMySQL Server 5.7include

添加库目录:C:Program FilesMySQLMySQL Server 5.7lib

360截图20141028210843540

2.链接器->输入:添加libmysql.lib

2

3.将libmysql.dll(在MySQLMySQL Server 5.7lib目录下)放到工程目录Debug下

4.建立一个数据库类MysqlDB,代码:

 1  1 #include "winsock2.h"  //this is mysqlDB.h
 2  2 #include "mysql.h"
 3  3 class mysqlDB{
 4  4 
 5  5 private:
 6  6     const char *host;
 7  7     const char *user;
 8  8     const char *pwd;
 9  9     const char *db;
10 10     bool flag;
11 11 
12 12 public:
13 13     MYSQL *con;
14 14     MYSQL_RES *res;
15 15     MYSQL_FIELD *field;
16 16     MYSQL_ROW row;
17 17 public:
18 18 
19 19     mysqlDB(const char *host, const char *user, const char *pwd, const char *db) :host(host), user(user),
20 20         pwd(pwd), db(db){};
21 21     bool connect();
22 22     bool tb_query(bool flag, char *sqlstr);
23 23 };
24 
25 #include "stdafx.h"
26 #include "mysqlDB.h"   //this is mysqlDB.cpp
27 
28 bool mysqlDB::connect()
29 {
30     con = mysql_init(0);
31     if (!mysql_real_connect(con, host, user, pwd, db, 3306, NULL, 0)) return false;
32     else return true;
33 }
34 bool mysqlDB::tb_query(bool flag, char *sqlstr)
35 {
36 
37     if (flag)  {
38         mysql_query(con, sqlstr);
39         res = mysql_store_result(con);
40     }
41     else return false;
42     return true;
43 }
44   
原文地址:https://www.cnblogs.com/qianzhilan/p/4057950.html