search
HomeDatabaseMysql Tutorialc/c++使用VS2013连接MySQL_MySQL

vs连接数据库其实就是将mysql数据库.h头文件接口、lib链接文件和dll执行文件加入到项目中。下面是配置如何加入。

转于http://www.cnblogs.com/justinzhang/archive/2011/09/23/2185963.html

一、VS2013工程设置工作

首先,建立一个windows应用程序的工程,将C/C++->预处理器->预处理器定义下的_WINDOWS改为_CONSOLE,

image

将连接器->系统->子系统 选择为控制台。

image

由于我们要使用Mysql的API,并且我们机子上肯定安装了Mysql数据库,所以我们要将工程的头文件路径指向Mysql安装目录的同文件mysql.h所在的位置,将连接库路径指向libmysql.lib所在的路径,

在我的机子上,Mysql 的安装路径为:C:\Program Files\MySQL\MySQL Server 5.1

image

image

我们需要把VS2008的工程中的头文件路径和连接库路径指向上面的两个地方:

将x项目属性页的C/C++->常规->附加包含目录指向:C:\Program Files\MySQL\MySQL Server 5.1\include

image

将项目属性页的链接器->常规->附加库目录指向:C:\Program Files\MySQL\MySQL Server 5.1\lib\opt.

image

将链接器->输入->附加依赖项中添加libmysql.lib。

image


如果不设置链接器->输入->附加依赖项中添加libmysql.lib,那么会出现如下的错误:

1>------ 已启动全部重新生成: 项目: MySql-Connect, 配置: Debug Win32 ------
1>正在删除项目“MySql-Connect”(配置“Debug|Win32”)的中间文件和输出文件
1>正在编译...
1>MySql_Connect.cpp
1>x:\编程练习\c-c++\c\mysql_connect.cpp(35) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\program files\microsoft visual studio 9.0\vc\include\stdio.h(306) : 参见“scanf”的声明
1>x:\编程练习\c-c++\c\mysql_connect.cpp(72) : warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\program files\microsoft visual studio 9.0\vc\include\stdio.h(366) : 参见“sprintf”的声明
1>x:\编程练习\c-c++\c\mysql_connect.cpp(86) : warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\program files\microsoft visual studio 9.0\vc\include\stdio.h(366) : 参见“sprintf”的声明
1>正在编译资源清单...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>正在链接...
1>LINK : 没有找到 d:\我的文档\Visual Studio 2008\Projects\MySql-Connect\Debug\MySql-Connect.exe 或上一个增量链接没有生成它;正在执行完全链接
1>MySql_Connect.obj : error LNK2019: 无法解析的外部符号 _mysql_close@4,该符号在函数 _main 中被引用
1>MySql_Connect.obj : error LNK2019: 无法解析的外部符号 _mysql_free_result@4,该符号在函数 _main 中被引用
1>MySql_Connect.obj : error LNK2019: 无法解析的外部符号 _mysql_num_fields@4,该符号在函数 _main 中被引用
1>MySql_Connect.obj : error LNK2019: 无法解析的外部符号 _mysql_fetch_row@4,该符号在函数 _main 中被引用
1>MySql_Connect.obj : error LNK2019: 无法解析的外部符号 _mysql_store_result@4,该符号在函数 _main 中被引用
1>MySql_Connect.obj : error LNK2019: 无法解析的外部符号 _mysql_error@4,该符号在函数 _main 中被引用
1>MySql_Connect.obj : error LNK2019: 无法解析的外部符号 _mysql_real_query@12,该符号在函数 _main 中被引用
1>MySql_Connect.obj : error LNK2019: 无法解析的外部符号 _mysql_select_db@8,该符号在函数 _main 中被引用
1>MySql_Connect.obj : error LNK2019: 无法解析的外部符号 _mysql_real_connect@32,该符号在函数 _main 中被引用
1>MySql_Connect.obj : error LNK2019: 无法解析的外部符号 _mysql_init@4,该符号在函数 _main 中被引用
1>d:\我的文档\Visual Studio 2008\Projects\MySql-Connect\Debug\MySql-Connect.exe : fatal error LNK1120: 10 个无法解析的外部命令
1>生成日志保存在“file://d:\我的文档\Visual Studio 2008\Projects\MySql-Connect\MySql-Connect\Debug\BuildLog.htm”
1>MySql-Connect - 11 个错误,3 个警告
========== 全部重新生成: 成功 0 个,失败 1 个,跳过 0 个 ==========


如果到这里你还没成功,继续看下面的解析:

之前经过配置,成功的在vs2013中成功的用c语言连接上的MySQL数据库,但是最近连接MySQL数据库,无论怎么配置,老是出错。

代码如下:

<ol class="dp-cpp"><li class="alt">#include <windows.h>  #include <stdio.h>  <li class="alt">#include <string.h> #include <mysql.h> <li class="alt"> #pragma comment (lib, "libmysql.lib") <li class="alt">#pragma comment (lib, "mysqlclient.lib")  <li class="alt">int main()  { <li class="alt">    char szTargetDSN[] = "test";     char szSqlText[500]=""; <li class="alt">    MYSQL * myData;     myData = mysql_init((MYSQL*)0); <li class="alt">         //连接数据库  <li class="alt">    if(mysql_real_connect( myData, NULL, "root", "123456", szTargetDSN, MYSQL_PORT, NULL, 0))     { <li class="alt">        printf("数据库连接成功!/n");         //构造SQL语句 <li class="alt">        sprintf(szSqlText,  "create table mytable" "(time datetime, s1 char(6), " "s2 char(11), s3 int, s4 int)");          if (mysql_query( myData, szSqlText)) <li class="alt">        {//执行SQL语句出错              printf( "Can't create table"); <li class="alt">            mysql_close( myData );             return FALSE; <li class="alt">        }         printf("表创建成功/n"); <li class="alt">        mysql_close(myData);     } <li class="alt">         return TRUE; <li class="alt">} 

错误如下:

error LNK2019: 无法解析的外部符号 _mysql_real_connect@32,该符号在函数 _main 中被引用

error LNK2019: 无法解析的外部符号 _mysql_query@8,该符号在函数 _main 中被引用

error LNK2019: 无法解析的外部符号 _mysql_init@4,该符号在函数 _main 中被引用

error LNK2019: 无法解析的外部符号 _mysql_close@4,该符号在函数 _main 中被引用

采用了以下的办法:

点击

1.项目->属性->vc++目录。
然后在"包含目录"中添加"E:/Program Files/MySQL/MySQL Server 5.5/include"
“库目录”中添加"E:/Program Files/MySQL/MySQL Server 5.5/lib"和"E:/Program Files/MySQL/MySQL Server 5.5/lib/Debug"。

2.项目->属性->链接器->输入->附加依赖项中添加libmysql.lib

但是编译依旧还是同样的问题。

于是开始思考,

lib是编译时需要的,dll是运行时需要的。

如果要完成源代码的编译,有lib就够了。

如果也使动态连接的程序运行起来,有dll就够了。

在开发和调试阶段,当然最好都有。

一般的动态库程序有lib文件和dll文件。lib文件是必须在编译期就连接到应用程序中的,而dll文件是运行期才会被调用的。如果有dll文件,那么对应的lib文件一般是一些索引信息,具体的实现在dll文件中。如果只有lib文件,那么这个lib文件是静态编译出来的,索引和实现都在其中。静态编译的lib文件有好处:给用户安装时就不需要再挂动态库了。但也有缺点,就是导致应用程序比较大,而且失去了动态库的灵活性,在版本升级时,同时要发布新的应用程序才行。

1.编译是通过静态链接库(lib)去找到接口的。

2.#pragma comment (lib, "libmysql.lib")

#pragma comment (lib, "mysqlclient.lib")

但是这两句代码并没有报错,证明这两个链接库也正常加入了啊。怎么还是出现“无法解析的外部符号”,很纳闷。

百思不得其解,踏遍百度谷歌必应。还是木有办法,或许就是那么灵光一闪,我擦。突然想起一个问题了。哥哥我装的是win7 64位啊,MySQL也是赤裸裸的64位,丫的,我用WIN32 项目搞毛线。于是有一个猜想就是,MySQL 64位的lib也是64位的接口。

于是用了两部去证明这个想法,

.项目->属性->配置管理器

活动解决方案平台,下拉选新建,出现一个新的对号框,在键入选择新平台中选择X64

最后重新编译,这次完全证明的我想法是对的。编译成功。哦也!

最后分析一下解决这个问题关键,其实这个问题很简单。搞明白dll和lib的作用,或许都能分析出这个问题了原因了。

写到这,再去搜索果不其然

http://www.linuxso.com/sql/19105.html

再一次说明,遇到问题要善于思考。

本文出自 “小桥流水的技术博客” 博客,请务必保留此出处http://idear.blog.bitsCN.com/4097017/871174

最后,也可以直接将.h文件和dll文件加到你的工程目录里。

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 InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

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),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor