MySQL connector/C ++

Source: Internet
Author: User

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 ++.

  1. # Include "stdafx. H"
  2. # Include <mysql_connection.h>
  3. # Include <mysql_driver.h>
  4. # Include <statement. h>
  5. Using NamespaceSQL;
  6. Using NamespaceSTD;
  7. VoidRunconnectmysql ()
  8. {
  9. MySQL: mysql_driver * driver;
  10. Connection * con;
  11. Statement * State;
  12. Resultset * result;
  13. // Initialize the driver
  14. Driver = SQL: mysql: get_mysql_driver_instance ();
  15. // Create a link
  16. Con = driver-> connect ("TCP: // 127.0.0.1: 3306","Root","123");
  17. State = con-> createstatement ();
  18. State-> execute ("Use test");
  19. // Query
  20. Result = state-> executequery ("Select * From testuser where ID <1002");
  21. // Output Query
  22. While(Result-> next ())
  23. {
  24. IntId = Result-> getint ("ID");
  25. String name = Result-> getstring ("Name");
  26. Cout <id <":"<Name <Endl;
  27. }
  28. DeleteState;
  29. DeleteCon;
  30. }
  31. Int_ Tmain (IntArgc, _ tchar * argv [])
  32. {
  33. Runconnectmysql ();
  34. Getchar ();
  35. Return0;
  36. }
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.