Recently, the project database management system was migrated from SQLSERVER2000 to MySQL. Previously, the database management system was connected to SQLSERVER Based on ADO. After using the MySQL database management system, the database creation, table creation, and database read/write operations can be quickly created using object-oriented encapsulation directly on the MySQL C language API. Currently, no
Recently, the project database management system was migrated from SQL SERVER2000 to MySQL. After connecting to SQL SERVER Based on ADO and using the MySQL database management system, the database creation, table creation, and database read/write operations can be quickly created using object-oriented encapsulation directly on the MySQL C language API. Currently, no
Recently, the project database management system was migrated from SQL SERVER2000 to MySQL. After connecting to SQL SERVER Based on ADO and using the MySQL database management system, the database creation, table creation, and database read/write operations are encapsulated in the MySQL C language API in an object-oriented manner. Currently, no connection pool module and transaction processing are added.
The source code is hosted on github.: Https://github.com/figot/MySQLWrapper
1. Features of MySQL
It is written in C and C ++ and tested using multiple compilers to ensure source code portability.
Supports multiple operating systems, including AIX, BSDi, FreeBSD, HP-UX, Linux, Mac OS, Novell NetWare, NetBSD, OpenBSD, OS/2 Wrap, Solaris, and Windows.
Provides APIs for multiple programming languages. These programming languages include C, C ++, C #, VB. NET, Delphi, Eiffel, Java, Perl, PHP, Python, Ruby, and Tcl.
Supports multiple failover processes, making full use of CPU resources, and supporting multiple users.
The simplified SQL query algorithm effectively improves the query speed.
It can run as a separate application in the network environment of the client server, and can also be embedded into other software as a library.
Multi-language support is provided. Common codes such as Chinese GB 2312, BIG5, and Japanese Shift JIS can all be used as data table names and data column names.
Provides multiple database connection methods, such as TCP/IP, ODBC, and JDBC.
Provides management tools for managing, checking, and optimizing database operations.
It can process large databases with tens of millions of records.
2. C ++ API Encapsulation
Using C ++ to connect to SQL has two direct interfaces: MySQL Connector/C ++ and MySQL ++, <1> MySQL Connector/C ++ is the latest MySQL Connector developed by Sun Microsystems. MySQL connector provides an object-oriented programming interface (API) for C ++ and a database drive connecting to the MySQL Server, which is different from the existing driver, connector/C ++ is the implementation of JDBC APIs in C ++. In other words, Connector/C ++ driver interfaces are mainly Java-based JDBC APIs. Java database connection (JDBC) is an industry standard for Java to connect to various databases. Connector/C ++ implements most of the JDBC 4.0 specifications. C ++ programmers familiar with JDBC programming can improve the efficiency of program development.
<2> MySQL ++ is a class library that uses C ++ to encapsulate the c api of MySQL. It is built on the Standard C ++ standard library (STL), making it as easy to process the database to process STL containers. In addition, MySQL ++ provides native C ++ interfaces that help you avoid repetitive work.
3. MySQL C ++ encapsulation implementation
In the process of quickly building a prototype, the two connection methods are not used and are directly encapsulated and implemented on MySQL c api.
# Ifndef _ MYSQL_INTERFACE_H __# define _ MYSQL_INTERFACE_H __# include "winsock. h" # include
# Include
# Include "mysql. h" # include
# Include
# Pragma comment (lib, "ws2_32.lib") # pragma comment (lib, "libmysql. lib") using namespace std; class MySQLInterface {public: MySQLInterface (); virtual ~ MySQLInterface (); bool connectMySQL (char * server, char * username, char * password, char * database, int port); bool createDatabase (std: string & dbname ); bool createdbTable (const std: string & query); void error1_mysql (); bool writeDataToDB (string queryStr); bool getDatafromDB (string queryStr, std: vector
> & Data); void closeMySQL (); public: int errorNum; // error code const char * errorInfo; // error message private: MYSQL mysqlInstance; // MySQL object, A Required Data Structure MYSQL_RES * result; // It is recommended to use a char * array to store the result}; # endif
# Include "stdafx. h "# include" MySQLInterface. h "// constructor initializes various variables and data. MySQLInterface: MySQLInterface (): errorNum (0), errorInfo (" OK ") {mysql_library_init (0, NULL, NULL ); mysql_init (& mysqlInstance); mysql_options (& mysqlInstance, MYSQL_SET_CHARSET_NAME, "gbk");} MySQLInterface ::~ MySQLInterface () {}// connect to MySQLbool MySQLInterface: connectMySQL (char * server, char * username, char * password, char * database, int port) {if (mysql_real_connect (& mysqlInstance, server, username, password, database, port, 0, 0 )! = NULL) return true; elseerror1_mysql (); return false;} // determines whether the database exists. If no database exists, create a database and open bool MySQLInterface: createDatabase (std :: string & dbname) {std: string queryStr = "create database if not exists"; queryStr + = dbname; if (0 = mysql_query (& mysqlInstance, queryStr. c_str () {queryStr = "use"; queryStr + = dbname; if (0 = mysql_query (& mysqlInstance, queryStr. c_str () {return true ;}} error1_mysql (); return false ;}// determine whether a table exists in the Database. If no table exists, create bool MySQLInterface :: createdbTable (const std: string & query) {if (0 = mysql_query (& mysqlInstance, query. c_str () {return true;} error1_mysql (); return false;} // write data bool MySQLInterface: writeDataToDB (string queryStr) {if (0 = mysql_query (& mysqlInstance, queryStr. c_str () return true; elseerror1_mysql (); return false;} // read data bool MySQLInterface: getDatafromDB (string queryStr, std: vector
> & Data) {if (0! = Mysql_query (& mysqlInstance, queryStr. c_str () {error1_mysql (); return false;} result = mysql_store_result (& mysqlInstance); int row = mysql_num_rows (result); int field = mysql_num_fields (result ); MYSQL_ROW line = NULL; line = mysql_fetch_row (result); int j = 0; std: string temp; while (NULL! = Line) {std: vector
Linedata; for (int I = 0; I