If I want to use C ++ to connect to the mysql database, first download MySQL connector/C ++ from the MySQL official website, and then configure the following method before connecting to mysql, for more information, see.
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. (The same is true for vs2010)
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. You can also directly use phpmyadmin .)
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 .)
The Code is as follows: |
Copy code |
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 '); |
Below is the test code of c ++:
The Code is as follows: |
Copy code |
# Include <mysql_connection.h> # Include <mysql_driver.h> # Include <statement. h> Using namespace SQL; Using namespace std; Void RunConnectMySQL () { 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 ("http: // 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 ()) { Int id = result-> getInt ("ID "); String name = result-> getString ("name "); Cout <id <":" <name <endl; } Delete state; Delete con; } Int main (int argc, char * argv []) { RunConnectMySQL (); Getchar (); Return 0; } |