There is no mysql connector cpp package in msys2 like mingw-w64-x86_64-libmariadbclient which is for mariadb.
I am trying to connect to mysql db from c++, I have installed cpp via msys2.
There is no mysql connector cpp package in msys2 like mingw-w64-x86_64-libmariadbclient which is for mariadb.
I am trying to connect to mysql db from c++, I have installed cpp via msys2.
On
I have managed to compile mysql program with the help of mingw-w64-ucrt-x86_64-libmariadbclient in my msys2.
Below is the sample program I compiled and ran on windows.
#include <iostream>
#include <mysql/mysql.h>
using namespace std;
int main()
{
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
conn = mysql_init(0);
conn = mysql_real_connect(conn, "localhost", "root", "", "user", 0, NULL, 0);
if (conn)
{
cout << " Connection to database successful:" << endl;
}
else
{
cout << "Connection unsuccessful" << endl;
}
// Execute a simple query
if (mysql_query(conn, "SELECT * FROM user")) {
std::cerr << "Error querying database: " << mysql_error(conn) << std::endl;
return 1;
}
// Get the result
res = mysql_store_result(conn);
// Get column information
MYSQL_FIELD *field;
while ((field = mysql_fetch_field(res))) {
std::cout << field->name << "\t";
}
std::cout << std::endl;
// Print the results
while ((row = mysql_fetch_row(res)) != NULL) {
for (unsigned int i = 0; i < mysql_num_fields(res); i++) {
if (row[i] != NULL) {
std::cout << row[i] << "\t";
} else {
std::cout << "NULL\t";
}
}
std::cout << std::endl;
}
// Clean up
mysql_free_result(res);
mysql_close(conn);
return 0;
}
The command to compile above program is g++ mysql.cpp -o mysql -lmariadb.
mysql.cpp is the name of the program file.
You answered your own question: the MariaDB client is binary-compatible with MySQL, so ostensibly you can use
mingw-w64-x86_64-libmariadbclientwith a MySQL server:https://mariadb.com/kb/en/mariadb-vs-mysql-compatibility/#:~:text=MariaDB's%20client%20protocol%20is%20binary,Perl%2C%20Python%2C%20Java%2C%20.