Use MySQL connector/C ++ to connect to the MySQL database

Source: Internet
Author: User

First, go to the MySQL official website to download MySQL connector/C ++

Http://dev.mysql.com/downloads/connector/cpp/1.0.html

Download the second package, Windows 32-bit non-installed version (I personally think this package is clean ). The current version is connector/C ++ 1.0.5.

Windows (x86, 32-bit), ZIP Archive
(Mysql-connector-c00000000-noinstall-1.0.5-win32.zip)

Decompress the entire package to the source file directory in the project folder. The folder name is too long. Change "mysql-connector-C ++-noinstall-1.0.5-win32" to "MySQL ".

Configure the vs2008 environment.

1. Project properties page-> C/C ++-> General-> additional include directories. Add the MySQL/include directory and the MySQL/include/cppconn directory.

2. Project properties page-> linker-> General-> additional library directories. Add the MySQL/lib directory.

3. Project properties page-> linker-> input-> additional dependencies. Add these two mysqlcppconn. Lib, mysqlcppconn-static.lib (two. Lib files under the MySQL/lib directory)

4. Copy the mysqlcppconn. dll file under MySQL/lib to the Windows/system32 folder.

Environment configuration is complete.

 

Create a table before connecting to the database.
(In fact, this can be done in the code. I want to make the test code as concise and easy as possible to identify errors)

Open the console, enter mysql-u root-P, and enter the password.

View the existing database. (';' Is added at the end of the SQL statement to execute the current statement immediately .)
Mysql> show databases;

Create a database
Mysql> Create Database test;

Use the database (this sentence cannot add points)
Mysql> use test

View existing tables
Mysql> show tables;

Create a table
Mysql> Create Table testuser (ID int, name char (20 ));

Insert data
Mysql> insert into testuser (ID, name) values (1001, 'Google ');
Mysql> insert into testuser (ID, name) values (1002, 'kingsoft ');
Mysql> insert into testuser (ID, name) values (1003, 'Firefox ');

 

Query the data in C ++.

# Include "stdafx. H "<br/> # include <mysql_connection.h> <br/> # include <mysql_driver.h> <br/> # include <statement. h> <br/> using namespace SQL; <br/> using namespace STD; <br/> void runconnectmysql () <br/>{< br/> MYSQL :: mysql_driver * driver; <br/> connection * con; <br/> statement * State; <br/> resultset * result; <br/> // initialize the driver <br/> driver = SQL: mysql: get_mysql_driver_instance (); <br/> // create a link <br/> con = driver-> connect ("TCP: // 127.0.0.1: 3306", "root", "123 "); <br/> state = con-> createstatement (); <br/> state-> execute ("use test "); <br/> // query <br/> result = state-> executequery ("select * From testuser where ID <1002 "); <br/> // output query <br/> while (result-> next ()) <br/>{< br/> int id = Result-> getint ("ID"); <br/> string name = Result-> getstring ("name "); <br/> cout <id <":" <name <Endl; <br/>}< br/> Delete state; <br/> Delete con; <br/>}< br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> runconnectmysql (); <br/> getchar (); <br/> return 0; <br/>}

 

It looks very convenient ~

In addition, I found two commonly used APIs when searching for C ++ APIs on the MySQL official website. One is very mature MySQL ++. It is said that it has experienced several changes over the years and is well received; the other is the MySQL ctor/C ++ introduced here (I had mixed up before, and later found that these two are not the same thing), which was just generated in the last two years and imitated JDBC, encapsulation is very convenient to use.

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.