C + + connection MySQL database instance code

Source: Internet
Author: User
Tags win32 zip create database


Download the second package, the WINDOWS32 bit is not installed (personally feel this bag clean). The current version is connector/c++ 1.0.5.
Windows (x86, 32-bit), ZIP Archive (Mysql-connector-c++-noinstall-1.0.5-win32.zip)

Extract the entire package to the source file directory under the project folder. Folder name is too long, change "Mysql-connector-c++-noinstall-1.0.5-win32" to "MySQL".

The following is to configure the VS2008 environment. (VS2010 is also the same as this operation)
1. Project property page->c/c++->general->additional Include directories. Add the Mysql/include directory and the Mysql/include/cppconn directory.

2. Project property page->linker->general->additional Library directories. Add the Mysql/lib directory.

3. Project property 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 complete.

Create a table before connecting to the database. (In fact, these can be done in the code, I am in order to make the test code as concise and easy to check the wrong, you can directly use phpMyAdmin.) )

Open the console, enter Mysql-u root-p, and enter a password.
View the currently existing database. (add '; ' at end of SQL statement) Represents the immediate execution of the current statement. )

The code is as follows Copy Code
mysql> show databases;
Creating a Database
mysql> CREATE DATABASE test;
Use database (this sentence cannot be semicolon)
mysql> Use test
View existing tables
Mysql> Show tables;
Create a table
Mysql> CREATE TABLE TestUser (ID INT, name CHAR (20));
Inserting 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 C + + test code:

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;
Initialization 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");
Inquire
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;
}

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.