C ++ encapsulation of MySQL

Source: Internet
Author: User

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.

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.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 32 33 34 35 36 38 39 # Ifndef _ MYSQL_INTERFACE_H _ # define _ MYSQL_INTERFACE_H _ # include "winsock. h "# include <iostream> # include <string> # include" mysql. h "# include <vector> # include <string> # 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 <std :: vector <std: string >>& 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 the char * array to store the result}; # endif

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 32 33 34 35 36 38 39 40 41 42 43 44 45 46 48 49 50 51 52 54 55 57 58 59 60 61 62 63 64 65 66 67 68 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 # 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 MySQL bool 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; else error1_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;} errorInto MySQL (); return false;} // determines whether a table exists in the Database. If no table exists, create the table 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; else error1_mysql (); return false;} // read data bool MySQLInt Erface: getDatafromDB (string queryStr, std: vector <std: string> & 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 <std: string> linedata; for (int I = 0; I <field; I ++) {if (line [I]) {temp = line [I]; linedata. push_back (temp);} else {temp = ""; linedata. push_back (temp) ;}} line = mysql_fetch_row (result); data. push_back (linedata) ;}return true ;}// error message void MySQLInterface: error1_mysql () {errorNum = mysql_errno (& mysqlInstance); errorInfo = mysql_error (& mysqlInstance );} // disconnect void MySQLInterface: closeMySQL () {mysql_close (& mysqlInstance );}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.