C + + connection MySQL

Source: Internet
Author: User
Tags mysql commands

In general, using Microsoft's own SQL Server in VS is a common practice, but SQL Server is only suitable for learning and not for real applications. Here, we select MySQL as the background database. The C + + language itself does not provide access to the database, generally using the drive to access the database. Microsoft provides an ODBC standard API for masking the underlying details of each database and providing a unified interface to access each database. On the MySQL official web site can also be called "CONNECTOR/ODBC" driver, but "connector/c++" driver should be more efficient than ODBC.

First, download and install the following software (official website download):

MySQL Server:mysql-5.1.73-win32.msi

MySQL Connector:mysql-connector-c++-1.1.4-win32.msi

MySQL Workbench:mysql-workbench-community-6.2.3-win32.msi (visual component)

Note: MySQL official website: http://www.mysql.com; can try wampserver+vs2010 (c + +)

Second, create a new control project with VS2010 and configure its properties:

1. Configuration Properties-->c/c++--> General-and additional Include directories:

C:\Program Files (x86) \mysql\mysql Connector C + + 1.1.4\include; C:\Program Files (x86) \mysql\mysql Connector C + + 1.1.4\include\cppconn;

2. Configure the properties---the linker--general--and additional library directories:

C:\Program Files (x86) \mysql\mysql Connector C + + 1.1.4\lib\opt; C:\Program Files (x86) \mysql\mysql Connector C + + 1.1.4\lib\debug;

3. Configure the properties---the linker--and input--Additional dependencies:

Mysqlcppconn.lib;mysqlcppconn-static.lib

4. Copy the 1.1.4\lib\opt file under C:\Program files (x86) \mysql\mysql Connector C + + Mysqlcppconn.dll to the system driver. Windows\System32 Directory

Third, build a table in MySQL (pre-installed MySQL Server):

In run, enter CMD, open the Command Line window (or click "Start"->mysql->mysql 5.1->mysql command lines Client), enter Mysql-u root-p, and then enter the password (the default password is empty);

View currently existing database:mysql> show databases;
Creating a database:mysql> create databases test1;
Use the database (this sentence cannot be semicolon):mysql> using test1
View existing tables:mysql> show tables;
CREATE TABLE test1 (ID INT, name CHAR (20)) for:mysql>.
Insert data:
mysql> INSERT into test1 (ID, name) VALUES (1001, ' Google ');
mysql> INSERT into test1 (ID, name) VALUES (1002, ' Kingsoft ');
mysql> INSERT into test1 (ID, name) VALUES (1003, ' Firefox ');

Note: add '; ' at the end of the SQL statement Represents the immediate execution of the current statement, and the operational database can use the visual software MySQL Workbench

Common MySQL Commands:

//View MySQL versionMysql>Selectversion ();//Show All DatabasesMysql>show databases;//Working with DatabasesMysql>Use database_name;//Show all data tablesMysql>show tables;//Show data table structureMysql>describe table_name;//Create a databaseMysql>CREATE DATABASE database_name;//Deleting a databaseMysql>DROP database database_name;//Create a data tableMysql>Use database_name;mysql> CREATE TABLE table_name (field name VARCHAR ( -), Field name CHAR (1));//Delete a data tableMysql>DROP table table_name;//Query RecordsMysql>Select*from table_name;//importing. sql FilesMysql>Use database_name;mysql> Source c:/Mysql.sql//Change root passwordmysql> UPDATE mysql.user SET Password=password ('New Password') WHERE user='Root';//ExitMysql> quit

Iv. Add source code for the project source file:

#include <iostream>#include<mysql_driver.h>#include<mysql_connection.h>#include<cppconn/driver.h>#include<cppconn/exception.h>#include<cppconn/resultset.h>#include<cppconn/statement.h>#include<cppconn/prepared_statement.h>using namespaceSQL;using namespacestd;voidRunconnectmysql () {Mysql::mysql_driver*driver; Connection*con; Statement*State ; ResultSet*result; //Initialize driverDriver =sql::mysql::get_mysql_driver_instance (); //Establish linkscon = Driver->connect ("tcp://127.0.0.1:3306","Root",""); State= con->createstatement (); State->execute ("Use test1"); //Enquiryresult = State->executequery ("SELECT * from Test1");//where ID < 1006//Output Query     while(result->Next ()) {        intid = result->getint ("ID"); stringName = Result->getstring ("name"); cout<< ID <<" : "<< name <<Endl;    } Delete state; Delete con;}intMain () {runconnectmysql ();    GetChar (); return 0;}

Compile and run with the following results:

Note: You may encounter a boost error at compile time, also configure the Boost library, please refer to: http://www.cnblogs.com/gaohongchen01/p/4006920.html

C + + connection MySQL

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.