MySQL's C + + encapsulation

Source: Internet
Author: User
Tags bool error code mysql in create database

The recent Project database management system migrated from SQL SERVER2000 to MySQL, previously connected to SQL SERVER based on ADO, using the MySQL database management system, Directly on the MySQL C language API in an object-oriented way to achieve the creation of the database, table creation, database read and write operations to quickly build prototypes, there is no connection pool module and transaction processing.

Characteristics of 1.MySQL

Use C and C + +, and use a variety of compilers for testing to ensure the portability of the source code.

Supports AIX, BSDI, FreeBSD, HP-UX, Linux, Mac OS, Novell NetWare, NetBSD, OpenBSD, Os/2 Wrap, Solaris, Windows, and many other operating systems.

Provides APIs for a wide variety of thread languages. These process languages include C, C + +, C #, vb.net, Delphi, Eiffel, Java, Perl, PHP, Python, Ruby, and Tcl.

Support multiple threads, make full use of CPU resources, support multi-user.

The improved SQL query algorithm can effectively improve the query speed.

It can be run as a separate application in a client server network environment, and can be embedded into other software as a library of programs.

Provides multi-language support, common encodings such as Chinese GB 2312, BIG5, and Japanese shift JIS can be used as data table names and data column names.

Provides a variety of database connectivity pathways such as TCP/IP, ODBC, and JDBC.

Provides management tools for managing, reviewing, and optimizing database operations.

You can work with large databases that have thousands records on them.

API Encapsulation for 2.c++

There are two direct-use interfaces for connecting SQL in C + +: MySQL connector/c++ and mysql+ +,<1>mysql connector/c++ are the latest available MySQL connectors, developed by Sun Microsystems. MySQL Connector provides C + + object-oriented programming interfaces (APIs) and database drives to connect to MySQL server, unlike existing driver, connector/c++ is the implementation of the JDBC API in C + +. In other words, the interface of connector/c++ driver is primarily a JDBC API based on the Java language. Java Database Connectivity (JDBC) is the industry standard for Java connections to various databases. Connector/c++ implements most of the specifications of JDBC 4.0. C + + program developers who are familiar with JDBC programming can improve the efficiency of program development.

<2>mysql++ is a class library that encapsulates the C API for MySQL in C + +. It is the establishment of standard C + + standard library (STL), so that processing database processing STL container as easy. In addition, the MySQL + + provides a native C + + interface that allows you to avoid the most repetitive tasks.

3.MySQL of C + + encapsulation implementation

In the rapid prototyping process, there is no use of these two connection methods, directly on the MySQL C API encapsulation implementation.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

?

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-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 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" &nbsp; &nbsp;//constructors initialize variables and data mysqlinterface::mysqlinterface (): &nbsp;&nbsp;&nbsp;&nbsp;errornum (0), errorinfo ("OK") {&nbsp;&nbsp;&nbsp;&nbsp;mysql_library_init (0,NULL,NULL); &nbsp;&nbsp;&nbsp;&nbsp;mysql_init (&amp;mysqlinstance); &nbsp;&nbsp;&nbsp;&nbsp;mysql_options (&amp;mysqlinstance,mysql_set_charset_name, "GBK"); } &nbsp; Mysqlinterface::~mysqlinterface () {&nbsp;} &nbsp;//connection mysql bool Mysqlinterface::connectmysql (char* server, CH ar* username, char* password, char* database,int port) {&nbsp;&nbsp;&nbsp;&nbsp;if (&amp; mysqlinstance,server,username,password,database,port,0,0)!= (NULL) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;return true; &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errorintomysql (); &nbsp;&nbsp;&nbsp;&nbsp;return false; //Determine if the database exists and does not exist then create the database and open bool Mysqlinterface::createdatabase (std::string&amp; dbname) {&nbsp;&nbsp;&nbsp;&nbsp; std::string querystr = "Create database if not exists "; &nbsp;&nbsp;&nbsp;&nbsp;querystr + = dbname; &nbsp;&nbsp;&nbsp;&nbsp;if (0 = mysql_query (&amp;mysqlinstance,querystr.c_str ()) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;QUERYSTR = "Use"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;querystr + = dbname; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (0 = mysql_query (&amp;mysqlinstance,querystr.c_str ()) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;return true; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;errorintomysql (); &nbsp;&nbsp;&nbsp;&nbsp;return false; //Determine if the corresponding table exists in the database and does not exist create a table bool Mysqlinterface::createdbtable (const std::string&amp; query) {&nbsp;&nbsp;&nbsp;&nbsp; if (0 = = mysql_query (&amp;mysqlinstance,query.c_str ()) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsP;&nbsp;&nbsp;&nbsp;return true; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;errorintomysql (); &nbsp;&nbsp;&nbsp;&nbsp;return false; } &nbsp;//write data bool Mysqlinterface::writedatatodb (String querystr) {&nbsp;&nbsp;&nbsp;&nbsp;if (0==mysql_query &amp; Mysqlinstance,querystr.c_str ()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true; &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errorintomysql (); &nbsp;&nbsp;&nbsp;&nbsp;return false;&nbsp;&nbsp;}//Read data bool Mysqlinterface::getdatafromdb (String queryStr, std:: vector&lt;std::vector&lt;std::string&gt; &gt;&amp; data) {&nbsp;&nbsp;&nbsp;&nbsp;if (0!=mysql_query) (&amp; Mysqlinstance,querystr.c_str ()) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Errorintomysql (); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false; &NBSP;&NBSP;&NBSP;&NBSP} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;result=mysql_store_result (&amp;mysqlinstance); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;int Row=mysQl_num_rows (result); &nbsp;&nbsp;&nbsp;&nbsp;int field=mysql_num_fields (Result); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;mysql_row Line=null; &nbsp;&nbsp;&nbsp;&nbsp;line=mysql_fetch_row (result); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;int j=0; &nbsp;&nbsp;&nbsp;&nbsp;std::string temp; &nbsp;&nbsp;&nbsp;&nbsp;while (null!=line) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;std::vector&lt;std::string&gt; Linedata; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i=0; i&lt;field;i++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (Line[i]) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp = Line[i]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;linedata.push_ Back (temp); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp = ""; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;linedata.push_ Back (temp); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line=mysql_fetch_row (Result); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.push_back (Linedata); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;return true; } &nbsp;//error message void Mysqlinterface::errorintomysql () {&nbsp;&nbsp;&nbsp;&nbsp;errornum=mysql_errno (&amp; Mysqlinstance); &nbsp;&nbsp;&nbsp;&nbsp;errorinfo=mysql_error (&amp;mysqlinstance); } &nbsp;//disconnect void Mysqlinterface::closemysql () {&NBSP;&NBSp;&nbsp;&nbsp;mysql_close (&amp;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.