First, go to the MySQL official website to download MySQL connector/C ++
Http://dev.mysql.com/downloads/connector/cpp/
Download the corresponding version based on your system platform. 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.
2. Project properties page-> linker-> General-> additional library directories. Add the MySQL \ Lib and $ mysql \ bin directories.
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. Run the mysqlcppconn. dll file in MySQL \ Lib andMySQL \ bin \ libmysql. dllCopy to the windows \ system32 folder.
Environment configuration is complete.
Create a table before connecting to the database.
(In factCodeTo make the test code as concise and error-prone as possible)
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"
- # Include <mysql_connection.h>
-
- # Include <mysql_driver.h>
-
- # Include <statement. h>
-
- Using NamespaceSQL;
-
- Using NamespaceSTD;
-
- VoidRunconnectmysql ()
-
- {
- MySQL: mysql_driver * driver;
-
- Connection * con;
-
- Statement * State;
-
- Resultset * result;
-
- // Initialize the driver
-
- Driver = SQL: mysql: get_mysql_driver_instance ();
-
- // Create a link
- Con = driver-> connect ("TCP: // 127.0.0.1: 3306","Root","123");
-
- State = con-> createstatement ();
-
- State-> execute ("Use test");
-
- // Query
-
- Result = state-> executequery ("Select * From testuser where ID <1002");
- // Output Query
-
- While(Result-> next ())
-
- {
-
- IntId = Result-> getint ("ID");
-
- String name = Result-> getstring ("Name");
- Cout <id <":"<Name <Endl;
-
- }
-
- DeleteState;
-
- DeleteCon;
-
- }
-
- Int_ Tmain (IntArgc, _ tchar * argv [])
-
- {
- Runconnectmysql ();
-
- Getchar ();
-
- Return0;
-
- }