How to implement a mysql General Class in C++
This article is about how to achieve a mysql general class in C++. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Without saying much, let's take a look at it.
Commonly used functions
Initialize the API library: mysql_library_init ()
Initialization data structure: mysql_init ()
Options before connecting to the database: mysql_options ()
Error message: mysql_error ()
Connection function: mysql_real_connect ()
Execute the SQL statement: mysql_query ()
Rollover query result: mysql_store_result ()
Close the connection: mysql_close ()
Dbctrl.h
# ifndef _ _ DBCTRL_H__#define _ DBCTRL_H__#include "winsock.h" # include "iostream.h" # include # include "mysql.h" # pragma comment (lib, "ws2_32.lib") # pragma comment (lib, "libmysql.lib") using namespace std;class DBctrl {private: MYSQL mydata / / MySQL object, a necessary data structure MYSQL_RES * result; / / it is recommended to use char* array to transfer this result to public: int row; / / the number of rows of query results int field / / number of columns of the query result int autoNum; / / the value of the last auto_increment int errorNum; / / error code const char* errorInfo; / / error prompt int affectedRow / / number of rows affected by the last query DBctrl (); / / Constructor void DBtest (int flag); void DBerror (); void DBconn (char* server, char* username, char* password, char* database,int port) Void DBquery (string queryStr); void DBquery (string queryStr, char* data); void DBclose ();}; # endif
Dbctrl.cpp
# include "dbctrl.h" / / Constructor initializes variables and data DBctrl::DBctrl () {mysql_library_init (zero null); mysql_init (& mydata); mysql_options (& mydata,MYSQL_SET_CHARSET_NAME, "gbk"); row=0; field=0; autoNum=0; errorNum=0; errorInfo= "ok"; affectedRow=0 } / / the test information is displayed at compile time, and void DBctrl::DBtest (int flag) {char* message; switch (flag) {case 1: message= "Database OK!"; break is available when you comment out the theme of this function. Case 2: message= "Database error!"; break; case 3: message= "Query OK!"; break; case 4: message= "Query error!"; break Case 5: message= "data overflew!"; break; default: message=NULL; break;} if (messageboxes null) cout