I plan to configure various environments in the afternoon and try to use VS2019 and MySQL together. There were a lot of mistakes in the middle, and the configuration was successfully configured around 9pm, as shown in the picture below:
Next, let’s talk about the specific steps:
(1) First Prepare the VS2019 and MySQL software, which are available on their respective official websites, so I won’t go into details here;
(2) Find the installation directory of MySQL, as shown in the figure, and find these two folders.
# (3) Create a new project and then create a main.cpp file to prepare for the following configuration environment.
(4) Open the project properties, click on the VC directory, and in the include directory, add the include file path in the MySQL installation directory here, as shown in the following figure:
(5) On the property page, open C/C, select General, follow the same steps as above, add the include file path in the MySQL file in the additional include directory;
(6) Continue on the property page, click on the linker option, click on General, copy the lib path in the MySQL installation directory to the additional library directory;
(7) Continue in the linker on the property page, click the input option, and add the libmysql.lib file. Note that you only need to copy the name libmysql.lib, and there is no need to add a path. Similarly, this file is also in the lib directory in the mydql installation folder:
(8) Check the platform above the property page, select x64, x32 may cause errors
(9) Copy bin\libmysql.dll in the MySQL installation directory to c:\windows\system32:
After the installation is completed, you can write test code. The test code is as follows:
#include <stdio.h> #include <iostream> #include "my_global.h" #include "mysql.h" using namespace std; int main() { cout << "hello world!" << endl; MYSQL mysql; MYSQL_RES* res; MYSQL_ROW row; mysql_init(&mysql); mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "gbk"); //注意:参数一定要对上。 //第二个参数为主机地址localhost,第三个参数为用户名 //第四个参数为用户密码,第五个参数为连接的数据库 //第六个参数为MySQL的端口号3306 if (mysql_real_connect(&mysql, "localhost", "root", "123456", "myemployees", 3306, NULL, 0) == NULL) { cout << (mysql_error(&mysql)); } mysql_query(&mysql, "SELECT * from myemployees.employees"); res = mysql_store_result(&mysql); //显示数据 //给ROW赋值,判断ROW是否为空,不为空就打印数据。 while (row = mysql_fetch_row(res)) { printf("%s ", row[0]);//打印ID printf("%s ", row[1]);//打印ID cout << endl; } //释放结果集 mysql_free_result(res); //关闭数据库 mysql_close(&mysql); //停留等待 getchar(); system("pause"); return 0; }
During the running process, if an error as shown in the figure below occurs, you need to troubleshoot the problem of configuring environment variables.
1. Right-click the project properties and check the VC directory configuration;
2. Check the input in the linker Options
(3) I put the "libmysql.dll and libmysql.lib" files in the .cpp folder in the project.
These three steps can check most of the specific problems.
Be sure to note that the MySQL parameters should not be written incorrectly. Miswriting sometimes results in the following Access denied for user 'ODBC'@'localhost' (using password: NO). You can enter it in the bin directory of mysql. Command:
mysql -u root -p, check whether the database user name and password are correct. The correct input is as shown below:
The above is the detailed content of What are the common problems when connecting to MySQL database in VS2019?. For more information, please follow other related articles on the PHP Chinese website!