Home Database Mysql Tutorial c/c++使用VS2013连接MySQL_MySQL

c/c++使用VS2013连接MySQL_MySQL

Jun 01, 2016 pm 01:04 PM
c

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">} 
Copy after login

错误如下:

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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are constants in C language? Can you give an example? What are constants in C language? Can you give an example? Aug 28, 2023 pm 10:45 PM

A constant is also called a variable and once defined, its value does not change during the execution of the program. Therefore, we can declare a variable as a constant referencing a fixed value. It is also called text. Constants must be defined using the Const keyword. Syntax The syntax of constants used in C programming language is as follows - consttypeVariableName; (or) consttype*VariableName; Different types of constants The different types of constants used in C programming language are as follows: Integer constants - For example: 1,0,34, 4567 Floating point constants - Example: 0.0, 156.89, 23.456 Octal and Hexadecimal constants - Example: Hex: 0x2a, 0xaa.. Octal

VSCode and VS C++ IntelliSense not working or picking up libraries VSCode and VS C++ IntelliSense not working or picking up libraries Feb 29, 2024 pm 01:28 PM

VS Code and Visual Studio C++ IntelliSense may not be able to pick up libraries, especially when working on large projects. When we hover over #Include&lt;wx/wx.h&gt;, we see the error message "CannotOpen source file 'string.h'" (depends on "wx/wx.h") and sometimes, autocomplete Function is unresponsive. In this article we will see what you can do if VSCode and VSC++ IntelliSense are not working or extracting libraries. Why doesn't my Intellisense work in C++? When working with large files, IntelliSense sometimes

Fix Xbox error code 8C230002 Fix Xbox error code 8C230002 Feb 27, 2024 pm 03:55 PM

Are you unable to purchase or watch content on your Xbox due to error code 8C230002? Some users keep getting this error when trying to purchase or watch content on their console. Sorry, there's a problem with the Xbox service. Try again later. For help with this issue, visit www.xbox.com/errorhelp. Status Code: 8C230002 This error code is usually caused by temporary server or network problems. However, there may be other reasons, such as your account's privacy settings or parental controls, that may prevent you from purchasing or viewing specific content. Fix Xbox Error Code 8C230002 If you receive error code 8C when trying to watch or purchase content on your Xbox console

Recursive program to find minimum and maximum elements of array in C++ Recursive program to find minimum and maximum elements of array in C++ Aug 31, 2023 pm 07:37 PM

We take the integer array Arr[] as input. The goal is to find the largest and smallest elements in an array using a recursive method. Since we are using recursion, we will iterate through the entire array until we reach length = 1 and then return A[0], which forms the base case. Otherwise, the current element is compared to the current minimum or maximum value and its value is updated recursively for subsequent elements. Let’s look at various input and output scenarios for this −Input −Arr={12,67,99,76,32}; Output −Maximum value in the array: 99 Explanation &mi

China Eastern Airlines announces that C919 passenger aircraft will soon be put into actual operation China Eastern Airlines announces that C919 passenger aircraft will soon be put into actual operation May 28, 2023 pm 11:43 PM

According to news on May 25, China Eastern Airlines disclosed the latest progress on the C919 passenger aircraft at the performance briefing meeting. According to the company, the C919 purchase agreement signed with COMAC has officially come into effect in March 2021, and the first C919 aircraft has been delivered by the end of 2022. It is expected that the aircraft will be officially put into actual operation soon. China Eastern Airlines will use Shanghai as its main base for commercial operations of the C919, and plans to introduce a total of five C919 passenger aircraft in 2022 and 2023. The company stated that future introduction plans will be determined based on actual operating conditions and route network planning. According to the editor's understanding, the C919 is China's new generation of single-aisle mainline passenger aircraft with completely independent intellectual property rights in the world, and it complies with internationally accepted airworthiness standards. Should

C++ program to print spiral pattern of numbers C++ program to print spiral pattern of numbers Sep 05, 2023 pm 06:25 PM

Displaying numbers in different formats is one of the basic coding problems of learning. Different coding concepts like conditional statements and loop statements. There are different programs in which we use special characters like asterisks to print triangles or squares. In this article, we will print numbers in spiral form, just like squares in C++. We take the number of rows n as input and start from the top left corner and move to the right, then down, then left, then up, then right again, and so on and so on. Spiral pattern with numbers 123456724252627282982340414243309223948494431102138474645321120373635343312191817161514

The function of void keyword in C language The function of void keyword in C language Feb 19, 2024 pm 11:33 PM

void in C is a special keyword used to represent an empty type, which means data without a specific type. In C language, void is usually used in the following three aspects. The function return type is void. In C language, functions can have different return types, such as int, float, char, etc. However, if the function does not return any value, the return type can be set to void. This means that after the function is executed, it does not return a specific value. For example: voidhelloWorld()

For the first time in 23 years, C# wins the TIOBE 2023 Programming Language of the Year Award For the first time in 23 years, C# wins the TIOBE 2023 Programming Language of the Year Award Jan 11, 2024 pm 04:45 PM

According to the TIOBE Programming Community Index, one of the benchmarks for measuring the popularity of programming languages, it is evaluated by collecting data from engineers, courses, vendors and search engines around the world. The TIOBE Index in January 2024 was released recently, and the official programming language rankings for 2023 were announced. C# won the TIOBE 2023 Programming Language of the Year. This is the first time that C# has won this honor in 23 years. TIOBE's official press release stated that C# has been in the top 10 for more than 20 years. Now it is catching up with the four major languages ​​and has become the programming language with the largest growth in one year (+1.43%). It well-deserved to win this award. Ranked second are Scratch (+0.83%) and Fortran (+0

See all articles