📜 ⬆️ ⬇️

Connecting and working with MySQL in Visual C ++

Most recently, I needed to write a program for remote work with a muscle. On the Internet, if it is not insulting, I found only “download the bla-bla, connect via include, and here's an example!”. In practice, it did not work, I had to look for experts to explain. To correct this annoying lack of information, lay out a full description from "A" to "Z" for setting up and working in Visual C ++. So let's get started!

I set up Visual C ++ 2008 and 2010 in this way. Since there is no particular difference in the interface of different versions of the visual editor, the configuration is almost the same.

In order to connect the database, such as muscle, and play with it on a pure API, you must first install or install such software:
1. “Connector / C ++” for 32-bit applications 32-bit library set, and for 64, respectively, 64-bit set (you can download it here http://dev.mysql.com/downloads/connector/cpp / ).
2. Muscle distribution, I have MySQL Community Server 5.1.59 when installing, you need to choose either a full installation, or select the entire set itself, well, DevCpp is enough for development.

Now, we have all the libraries needed to create a program in Visual C ++. Next, we check the structure of our muscle server directories. My server is installed in D: \ programming \ MySQL Server 5.1 . The directory can be arbitrary, but it must contain the include and lib folders. Next, copy all the files from C: \ Program Files \ MySQL \ MySQL Connector C ++ 1.1.0 \ lib \ opt (folder with the installed connector) to D: \ programming \ MySQL Server 5.1 \ lib \ opt (folder with server).
')
File transfers are over, now let's configure C ++ itself:
1. Open Visual C ++, create a standard console project, save it to any convenient place.
2. Be sure (!!!) to choose the mode of building the project Release . If you choose the standard Debug , then you need to save the files in other directories.
3. Open the project properties (in the decision browser window, right-click on the project - properties).
4. Select “Configuration properties - / ++ - General - Additional directories of included files”, add the include directory from the server folder (in my case D: \ programming \ MySQL Server 5.1 \ include ).
6. In the project properties, select the item “Configuration Properties - C / C ++ - Linker - General - Additional Library Directories”, add the lib / opt directory (in my case D: \ programming \ MySQL Server 5.1 \ lib \ opt ).
7. In the project properties, select the item “Configuration properties - C / C ++ - Linker - Input - Additional dependencies”, add 2 values mysqlcppconn-static.lib and libmysql.lib .
8. In the project properties, select the item “Configuration Properties - C / C ++ - Preprocessor - Preprocessor Definitions”, add the value CPPCONN_PUBLIC_FUNC = .
9. Save the project properties.

Then all that remains is to add to the project #include <mysql.h> , and you can work!

Here are 2 simple examples of working with a muscle:

Test connection to the database
#include "stdafx.h"
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <mysql.h>
#include<stdio.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
MYSQL *conn;
//
conn = mysql_init(NULL);
if(conn == NULL)
{
// –
fprintf(stderr, "Error: can'tcreate MySQL-descriptor\n");
//exit(1); //
}
//
if(!mysql_real_connect(conn, "localhost", "root", "root", "test", NULL, NULL, 0))
{
//
//
fprintf(stderr, "Error: can'tconnecttodatabase %s\n", mysql_error(conn));
}
else
{
// - "Success!"
fprintf(stdout, "Success!\n");
}
//
mysql_close(conn);

system("Pause");
return 0;
}


Displaying data from a table
#include"stdafx.h"
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<mysql.h>

usingnamespacestd;

int_tmain(intargc, _TCHAR* argv[])
{

system("cls");

MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;

int i = 0;

//
conn = mysql_init(NULL);
if(conn == NULL)
{
// –
fprintf(stderr, "Error: can'tcreate MySQL-descriptor\n");
//exit(1); //
}
//
if(!mysql_real_connect(conn, "localhost", "root", "root", "test", NULL, NULL, 0))
{
//
//
fprintf(stderr, "Error: can'tconnecttodatabase %s\n", mysql_error(conn));
}
else
{
// - "Success!"
fprintf(stdout, "Success!\n");
}

mysql_set_character_set(conn, "utf8");
// , latin1
cout<<"connectioncharacterset: "<<mysql_character_set_name(conn) <<endl;

mysql_query(conn,"SELECT id, text FROM mnu"); // =)

if (res = mysql_store_result(conn)){
while(row = mysql_fetch_row(res)) {
for (i=0 ; i <mysql_num_fields(res); i++){
std::cout<<row[i] <<"\n"; //
}
}
} elsefprintf(stderr, "%s\n", mysql_error(conn));

//
mysql_close(conn);

system("Pause");

return 0;
}


PS Thanks to Vladimir Budilo for clarifying the topic, this article was published thanks to his explanations.

Source: https://habr.com/ru/post/131576/


All Articles