Under installed MySQL, the data table built on the premise.
If you simply want to add to achieve a data or other operational data, reference may be another blog. HTTPS: // www.cnblogs.com/ming-4/p/11544514.html
Cpp file to define a database management and header files.
MySQLManager.h file:
/*MySQLManager.h file: File name: MySQLManager.h contents: MySQL database connection management class creation date: October 18, 2016 Created: AceTan * / #pragma Once // network communication headers #include <WinSock.h > // introduced mysql header files (good practice is the folder copying project directory, you can also set the directory in vc) #include " the include / mysql.h " #include <the Windows.h> // include additional dependencies items may be in the works - set the properties inside #pragma the Comment (lib, "wsock32.lib") #pragma the Comment (lib, "libmysql.lib") // Some of the information necessary to connect to the database struct ConnectionInfo { const char Host *; // host addresses const char * the User; // username const char * password; // password const char * Database; // database name unsigned int Port; // port number const char * unix_socket; // UNIX connection identification unsigned Long clientflag; // client connection flag // constructor, set some default values the ConnectionInfo (): Host ( " 127.0.0.1 " ), Port ( 3306 ), unix_socket is (NULL), clientflag ( 0 ) { } }; class mysqlmanager { public : // connect to the database BOOL the Init (the ConnectionInfo & info); // releasable connection BOOL FreeConnect (); // increase the data // BOOL InsertData (const char * sql); // delete the data // BOOL the DeleteData (const char * sql); // update data // BOOL UpdateData (const char * sql); // execute sql statements, including add, delete, update data boolExecuteSQL ( const char * SQL); // query data A MYSQL_RES QueryData is * ( const char * SQL); // Prints the result set void PrintQueryRes (); Private : MYSQL Based m_mysql; // MySQL connector A MYSQL_RES * m_res; // query result set };
MySQLManager.cpp file:
#include <the iostream> #include <stdio.h> the using namespace STD; // connected database BOOL mysqlmanager :: the Init (the ConnectionInfo & info) { // initialize mysql, connected mysql, database mysql_init (& m_mysql); // connection failure if (! (mysql_real_connect (& m_mysql, info.host, info.user, info.password, info.database, info.port, info.unix_socket, info.clientflag))) { return to false ; } return to true ; } // release connecting BOOL mysqlmanager :: FreeConnect () { //Release resources mysql_free_result (m_res); mysql_close ( & m_mysql); return false ; } // execute sql statements, including add, delete, update data BOOL mysqlmanager :: ExecuteSQL ( const char * sql) { IF (mysql_query (& m_mysql, sql )) { // play error log, here directly to the console cerr << " execute sql statement fails, the error message is: " << mysql_error (& m_mysql) << endl; return false ; } the else { cout << " execution sql statement success!" << endl; } return to true ; } // query data MYSQL_RES * mysqlmanager :: QueryData ( const char * SQL) { IF (mysql_query (& m_mysql, SQL)) { // play error log, shown here directly to the console << cerr " query fails, the error message is: " << mysql_error (& m_mysql) << endl; return nullptr; } the else { cout << " query executed successfully! " << endl; Store query results m_res mysql_store_result = (& m_mysql); return m_res; } // traverse the result set void mysqlmanager :: PrintQueryRes () { IF (nullptr a m_res == == NULL || m_res) { return ; } // Get the number of rows // unsigned int = the mysql_affected_rows rows (m_mysql); // field column array The MYSQL_FIELD * = field nullptr a; // save the two-dimensional array of field names char the fieldName [ 64 ] [ 32 ]; // get the field name for ( int I =0 ; = Field The mysql_fetch_field (m_res); ++ I) { strcpy_s (the fieldName [I], Field -> name); } // Get the number of columns int Columns = mysql_num_fields (m_res); for ( int I = 0 ; I < Columns; ++ I) { // the printf formatted using the C language, a little more convenient the printf ( " % 10s \ T " , the fieldName [I]); } COUT << endl; MYSQL_ROW Row; the while (Row = mysql_fetch_row (m_res )) { for (int i = 0; i < columns; ++i) { printf("%10s\t", row[i]); } cout << endl; } }
main functions:
#include <the iostream> #include " MySQLManager.h " the using namespace STD; int main () { mysqlmanager MySQL; ConnectionInfo info; // filling ConnectionInfo this structure, generally project from the configuration file which is read info.user = " the root " ; info.password = " your_password " ; info.host = " localhost " ; info.port = 3306 ; info.database = " Test " ; info.unix_socket = NULL; info.clientflag = 0 ; // MySQL connection IF (! mysql.Init (info)) { return - . 1 ; } // increase test data const char * SQL1 = " INSERT INTO User values (NULL, 'Ada', 'password') " ; mysql.ExecuteSql (SQL1); // delete the test data const char * SQL2 = " the delete from the User the WHERE name = 'AceTan' " ; mysql.ExecuteSql (SQL2); // modify data test const char * SQL3 = " Update User password = SET 'update_password' WHERE name = 'Ada' " ; mysql.ExecuteSql (SQL3); // query test data const char * sql4 = " SELECT * from User " ; mysql.QueryData (sql4 ); mysql.PrintQueryRes (); // release resources mysql mysql.FreeConnect (); return 0 ; }