Use VS2013 to connect MySQL to C + +

Source: Internet
Author: User
Tags sprintf

VS Connect the database is actually the MySQL database. h header file interface, Lib link file, and DLL execution files are added to the project. Here is how the configuration is joined.



Go to http://www.cnblogs.com/justinzhang/archive/2011/09/23/2185963.html

First, VS2013 project setup work

First, build a project for a Windows application and change the _windows under the preprocessor definition c/c++-> preprocessor to _console,

Select the subsystem, System, connector, as the console.

Since we are going to use the MySQL API and we have a MySQL database installed on our machine, we will point the header file path of the project to the location of the same file mysql.h in the MySQL installation directory, point the Connection library path to the path where the Libmysql.lib is located,

On my machine, the installation path for Mysql is: C:\Program files\mysql\mysql Server 5.1

We need to point the header file path and the connection library path in the VS2008 project to the top two places:

Point to the c/c++-> general, additional include directory for the X Project property page: C:\Program files\mysql\mysql Server 5.1\include

Point to the Add-on library directory, general----the linker for Project Property Pages: C:\Program files\mysql\mysql Server 5.1\lib\opt.

Adds a libmysql.lib to the additional dependencies, input, linker.


If you do not set add Libmysql.lib in the additional dependencies, input-to-linker, the following error occurs:

1>------started rebuild All: project: mysql-connect, config: Debug Win32------
1> is deleting the project "Mysql-connect" (Configuration debug| Win32 ") Intermediate and output files
1> is compiling ...
1>mysql_connect.cpp
1>x:\ Programming Exercise \c-c++\c\mysql_connect.cpp: Warning C4996: ' scanf ': This function or variable could 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): See Declaration of "scanf"
1>x:\ Programming Exercise \c-c++\c\mysql_connect.cpp (a): Warning C4996: ' sprintf ': This function or variable could 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): See Declaration of "sprintf"
1>x:\ Programming Practice \c-c++\c\mysql_connect.cpp: Warning C4996: ' sprintf ': This function or variable could 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): See Declaration of "sprintf"
1> Compiling resource list ...
1>microsoft (R) Windows (r) Resource Compiler Version 6.1.6723.1
1>copyright (C) Microsoft Corporation. All rights reserved.
1> is linking ...
1>link: D:\ My document was not found \visual Studio 2008\projects\mysql-connect\debug\mysql-connect.exe or the previous incremental link did not generate it; full link is being performed
1>mysql_connect.obj:error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main
1>mysql_connect.obj:error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main
1>mysql_connect.obj:error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main
1>mysql_connect.obj:error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main
1>mysql_connect.obj:error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main
1>mysql_connect.obj:error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main
1>mysql_connect.obj:error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main
1>mysql_connect.obj:error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main
1>mysql_connect.obj:error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main
1>mysql_connect.obj:error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main
1>d:\ my document \visual Studio 2008\projects\mysql-connect\debug\mysql-connect.exe:fatal error lnk1120:10 an unresolved external command
1> build log saved in "File://d:\ My Documents \visual Studio 2008\projects\mysql-connect\mysql-connect\debug\buildlog.htm"
1>mysql-connect-11 a bug, 3 warnings
========== All rebuilds: 0 successful, 1 failed, 0 ========== skipped



If you are not successful here, continue to look at the following analysis:



Previously configured successfully in the vs2013 with the C language connection on the MySQL database, but recently connected to the MySQL database, no matter how the configuration, always error.

The code is as follows:

  
 
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <mysql.h>
  5. #pragma comment (lib, "Libmysql.lib")
  6. #pragma comment (lib, "Mysqlclient.lib")
  7. int main ()
  8. {
  9. Char sztargetdsn[] = "Test";
  10. Char szsqltext[500]= "";
  11. MYSQL * MYDATA;
  12. MyData = Mysql_init ((mysql*) 0);
  13. Connecting to a database
  14. if (MySQL_real_connect (myData, NULL, "root", "123456", Sztargetdsn, Mysql_port, NULL, 0))
  15. {
  16. printf (" database connection succeeded!") /n ");
  17. Constructing SQL statements
  18. sprintf (Szsqltext, "CREATE TABLE MyTable" "(Time datetime, S1 char (6)," "S2 char (one-by-one), S3 int, S4 int)");
  19. if (mysql_query (MyData, Szsqltext))
  20. {//Execute SQL statement error
  21. printf ("Can ' t create TABLE");
  22. Mysql_close (MyData);
  23. return FALSE;
  24. }
  25. printf ("Table created successfully/n");
  26. MySQL_close (myData);
  27. }
  28. return TRUE;
  29. }

The error is as follows:

Error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main

Error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main

Error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main

Error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main

The following approaches are used:

Click

1.VC + + directory for project, properties.
Then add "E:/program files/mysql/mysql Server 5.5/include" in the "Include directory"
Add "E:/program files/mysql/mysql server 5.5/lib" and "E:/program Files/mysql/mysql server 5.5/lib/debug" in the Library directory.

2. Add an additional dependency, input-------Properties---

But compiling is still the same problem.

Then began to think,

Lib is required at compile time, and DLLs are required at runtime.

If you want to complete the compilation of the source code , there is Lib enough.

If you also make a dynamically connected program run, there is enough DLL.

In the development and commissioning phase, of course, it is best to have.

The General Dynamic Library program has lib files and DLL files. Lib files must be connected to the application at compile time, and DLL files are not called until the run time. If there are DLL files, then the corresponding LIB file is generally some index information, specifically implemented in the DLL file. If only the Lib file, then the Lib file is statically compiled, the index and implementation are in it. A statically compiled LIB file has the advantage of not having to hang up the dynamic library when installing to the user. But there are drawbacks, that is, the application is larger, and the flexibility of the dynamic library is lost, and when the version is upgraded, a new application will be published at the same time.

1. Compilation is done through the static link library (Lib) to find the interface.

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

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

But these two code did not have the error, proves that these two link library also joins normally. Why still appear "unresolved external symbol", very puzzled.

Best not to its solution, stepping on Baidu Google Bing. Or wood there is a way, perhaps it is so brilliant flash, I rub. Suddenly I think of a problem. Brother I installed is Win7 64 Ah, MySQL is also naked 64, ya, I use the WIN32 project to engage in wool. So there is a conjecture that MySQL 64-bit Lib is also a 64-bit interface.

So I used two of them to prove the idea,

. Project, properties, Configuration Manager

Active Solution Platform, dropdown new, a new check box appears, select X64 in the type select New platform

Finally recompile, this time fully proved my idea is right. Compilation succeeded. Oh, too!

The final analysis of the key to solve the problem, in fact, the problem is very simple. Understand the role of DLL and LIB, may be able to analyze the cause of the problem.

Write it down and search for it.

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

Once again, you have to be good at thinking about problems.


This article is from the "Small Bridge Technology Blog" blog, please make sure to keep this source http://idear.blog.51cto.com/4097017/871174





Finally, you can add. h files and DLL files directly to your project directory.

Use VS2013 to connect MySQL to C + +

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.