Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to connect MySQL in C++

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

C++ how to connect MySQL, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

1. Why use MySQL C API

Efficiency! C++ can connect to the database in many ways. In addition to using MySQL C, you can also configure ODBC to access the database (http://blog.csdn.net/open520yin/article/details/8148509). But the way of MySQL C is the most direct, and I think it is very advantageous in terms of program efficiency or programming efficiency. C++ is characterized by high efficiency, so it is recommended to use MySQL C API.

2. The environment I use

System: Windows XP Professional SP3

IDE:Visual Studio 6.0 / Visual Studio 2010 Express

MySQL:MySQL Database Version 5.0.51b

3. Preparation before starting

1. Find the directory of the library file and header file:

/ mysql installation directory / Include / mysql installation directory / lib/opt

2. Copy the libmysql.dll from the lib folder to the desktop for backup

3. If you are using AppServ or other scaled-down versions of MySQL, you may not have library files and dll files. Download them here (or download them online): http://dl.dbank.com/c019eqfx5j

The files and folders extracted after download can be placed in any location.

4. The environment configuration under Visual C++ 6.0

Open Visual C++ 6.0, select tools-options from the menu bar, and move to the contents tab.

Add the library file directory of MySQL to Library Files:

Add the header file directory of MySQL to Include Files:

5. Environment configuration under Visual C++ 2010 Express

Instead of setting up a directory uniformly, vs2010 assigns a directory to the project after each project is created.

As shown in the following figure, right-click on the built project and select Properties

In the properties dialog box, locate the VC++ directory entry and set the containing directory (include) and library directory (lib)

6. Write a sample program

Basically, the environment is configured. Then go to MySQL to see if there is any problem, if not, create a new database named "test", standby.

Now compile the following sample code, and then put the previously mentioned libmysql.dll and the code in the same folder. After connection and execution, if "atabase connection OK!" is displayed. Indicates that the connection was successful.

The sample program code is as follows:

# include "winsock.h" # include "iostream.h" # include "mysql.h" # pragma comment (lib, "ws2_32.lib") # pragma comment (lib, "libmysql.lib") using namespace std; void main () {MYSQL mydata; mysql_library_init (zero null) / / initialize the MySQL C API library mysql_init (& mydata); / / initialize the data structure of mydata. Mydata is the MYSQL object mysql_options (& mydata,MYSQL_SET_CHARSET_NAME, "gbk"). / / connection option: add GBK character set support / / connection function if (mysql_real_connect (& mydata, "localhost", "root", "123456", "test", 3306memo)! = NULL) cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report