search
HomeDatabaseMysql TutorialVS2013下连接MySQL5.6小例

环境:win832系统,VS2013,MySQL5.6,boost1.60 需要注意的是,安装MySQL时需要安装完整版本,否则在MySql的目录下可能会没有Connector.C++ 1.1这个目录。 boost下载后,直接解压即可。(我是放在C:\Program Files目录下),下载地址:http://www.boost.org/u

环境:win832系统,VS2013,MySQL5.6,boost1.60
需要注意的是,安装MySQL时需要安装完整版本,否则在MySql的目录下可能会没有Connector.C++ 1.1这个目录。
boost下载后,直接解压即可。(我是放在C:\Program Files目录下),下载地址:http://www.boost.org/users/download/
连接的方式有2种,一种是纯C风格的,不需要使用Connector.C++ 1.1目录下提供的内容。(个人觉得麻烦,代码看着乱,网上有很多资源)。另一种就是利用Connector.C++ 1.1提供的内容,代码简洁,这里只讲这种方法连接数据库。

首先,新建一个VC++的win32空项目。添加源文件,代码如下(代码暂时无法运行,具体配置在后面)
源.cpp

<code class=" hljs lasso">
<span class="hljs-variable">#include</span><span class="hljs-subst"><</span>cppconn<span class="hljs-subst">\</span>driver<span class="hljs-built_in">.</span>h<span class="hljs-subst">></span>
<span class="hljs-variable">#include</span><span class="hljs-subst"><</span>cppconn<span class="hljs-subst">\</span>exception<span class="hljs-built_in">.</span>h<span class="hljs-subst">></span>
<span class="hljs-variable">#include</span> <span class="hljs-subst"><</span>cppconn/<span class="hljs-keyword">resultset</span><span class="hljs-built_in">.</span>h<span class="hljs-subst">></span> 
<span class="hljs-variable">#include</span> <span class="hljs-subst"><</span>cppconn/statement<span class="hljs-built_in">.</span>h<span class="hljs-subst">></span>
<span class="hljs-variable">#include</span><span class="hljs-subst"><</span>mysql_connection<span class="hljs-built_in">.</span>h<span class="hljs-subst">></span>
<span class="hljs-variable">#include</span><span class="hljs-subst"><</span>iostream<span class="hljs-subst">></span>
<span class="hljs-variable">#include</span><span class="hljs-subst"><</span><span class="hljs-built_in">string</span><span class="hljs-subst">></span>
using namespace std;
int main()
{

        sql<span class="hljs-tag">::Driver</span> <span class="hljs-subst">*</span>dirver;
        sql<span class="hljs-tag">::Connection</span> <span class="hljs-subst">*</span>con;
        sql<span class="hljs-tag">::Statement</span> <span class="hljs-subst">*</span>stmt;
        sql<span class="hljs-tag">::PreparedStatement</span> <span class="hljs-subst">*</span>pstmt;
        sql<span class="hljs-tag">::ResultSet</span> <span class="hljs-subst">*</span>res;
        dirver <span class="hljs-subst">=</span> get_driver_instance();
        <span class="hljs-comment">//连接数据库</span>
        con <span class="hljs-subst">=</span> dirver<span class="hljs-subst">-></span>connect(<span class="hljs-string">"localhost"</span>, <span class="hljs-string">"root"</span>, <span class="hljs-string">"123456"</span>);
        <span class="hljs-comment">//选择mydata数据库</span>
        con<span class="hljs-subst">-></span>setSchema(<span class="hljs-string">"mydata"</span>);
        con<span class="hljs-subst">-></span>setClientOption(<span class="hljs-string">"characterSetResults"</span>, <span class="hljs-string">"utf8"</span>);
        stmt <span class="hljs-subst">=</span> con<span class="hljs-subst">-></span>createStatement();
        <span class="hljs-comment">//从name_table表中获取所有信息</span>
        res <span class="hljs-subst">=</span> stmt<span class="hljs-subst">-></span>executeQuery(<span class="hljs-string">"SELECT * from name_table"</span>);
        <span class="hljs-comment">//循环遍历</span>
        <span class="hljs-keyword">while</span> (res<span class="hljs-subst">-></span>next())
        {
            <span class="hljs-comment">//输出,id,name,age,work,others字段的信息</span>
            cout <span class="hljs-subst"><<</span> res<span class="hljs-subst">-></span>getInt(<span class="hljs-string">"ID"</span>) <span class="hljs-subst"><<</span> <span class="hljs-string">" | "</span> 
                <span class="hljs-subst"><<</span> res<span class="hljs-subst">-></span>getString(<span class="hljs-string">"name"</span>) <span class="hljs-subst"><<</span> <span class="hljs-string">" | "</span> 
                <span class="hljs-subst"><<</span> res<span class="hljs-subst">-></span>getInt(<span class="hljs-string">"age"</span>) <span class="hljs-subst"><<</span> <span class="hljs-string">" | "</span> 
                <span class="hljs-subst"><<</span> res<span class="hljs-subst">-></span>getString(<span class="hljs-string">"work"</span>) <span class="hljs-subst"><<</span> <span class="hljs-string">" | "</span> 
                <span class="hljs-subst"><<</span> res<span class="hljs-subst">-></span>getString(<span class="hljs-string">"others"</span>) <span class="hljs-subst"><<</span> endl;

        }
        <span class="hljs-comment">//清理</span>
        delete res;
        delete stmt;
        delete con;

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}</code>

对于以Debug版本运行的程序:
1、项目(P)——xxx属性页——配置属性——C/C++——附加包含目录添加:
C:\Program Files\MySQL\Connector.C++ 1.1\include
C:\Program Files\boost_1_60_0
2、项目(P)——xxx属性页——配置属性——链接器——常规——附加库目录添加
C:\Program Files\MySQL\Connector.C++ 1.1\lib\debug
3、项目(P)——xxx属性页——配置属性——链接器——输入——附加依赖项添加
mysqlcppconn.lib
4、将C:\Program Files\MySQL\MySQL Server 5.6\lib目录下的libmysql.dllC:\Program Files\MySQL\Connector.C++ 1.1\lib\debug目录下的mysqlcppconn.dll,拷贝到工程目录中(和源文件放在一个目录即可)。

对于以Release版本运行的程序:
1、项目(P)——xxx属性页——配置属性——C/C++——附加包含目录添加:
C:\Program Files\MySQL\Connector.C++ 1.1\include
C:\Program Files\boost_1_60_0
2、项目(P)——xxx属性页——配置属性——链接器——常规——附加库目录添加
C:\Program Files\MySQL\Connector.C++ 1.1\lib\opt
3、项目(P)——xxx属性页——配置属性——链接器——输入——附加依赖项添加
mysqlcppconn.lib
4、将C:\Program Files\MySQL\MySQL Server 5.6\lib目录下的libmysql.dllC:\Program Files\MySQL\Connector.C++ 1.1\lib\opt目录下的mysqlcppconn.dll,拷贝到工程目录中(和源文件放在一个目录即可)。

为什么区别配置Debug和Release版本?
可以试一下,以本文的程序为例,以Debug的配置,在Release版本下运行不了程序。反之亦然。

运行结果:
数据库中的数据
这里写图片描述
程序运行结果
这里写图片描述

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL's Purpose: Storing and Managing Data EffectivelyMySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AM

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

SQL and MySQL: Understanding the RelationshipSQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AM

The relationship between SQL and MySQL is the relationship between standard languages ​​and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool