To connect to a MySQL database using C++
############################################
############################################
Work in progress...
https://stackoverflow.com/questions/28761323/how-to-include-external-library-boost-into-clion-c-project-with-cmake
https://stackoverflow.com/questions/36519453/setup-boost-in-clion
https://www.bogotobogo.com/cplusplus/libraries.php
https://stackoverflow.com/questions/10358745/how-to-use-libraries
https://cliutils.gitlab.io/modern-cmake/
https://stackoverflow.com/questions/34513122/clion-undefined-get-driver-instance
https://www.jetbrains.com/help/clion/2020.1/cmake0.html?utm_campaign=CL&utm_content=2020.1&utm_medium=link&utm_source=product
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206608335-How-can-I-specify-additional-include-files-
https://dev.mysql.com/doc/connector-cpp/8.0/en/connector-cpp-apps-general-considerations.html
############################################
############################################
1) Get the mySQL connector:
https://dev.mysql.com/downloads/connector/cpp/
2) Get the Boost C++ libraries:
https://formulae.brew.sh/formula/boost (Mac)
https://www.boost.org/users/history/version_1_73_0.html
SEE: https://blog.david14.com/2020/06/c-boost-libraries.html
3) Find MySQL connector:
On Mac;
/usr/local/mysql-connector-c++-8.0.20/include/jdbc
4) Find boost:
On Mac;
/usr/local/Cellar/boost/1.72.0_3/include/boost
5) Edit CmakeLists.txt to something similar to the below: ***** NOT CURRENT *****
cmake_minimum_required(VERSION 3.16)
project(mySQL_Example)
set(CMAKE_CXX_STANDARD 11)
include_directories(/usr/local/mysql-connector-c++-8.0.20/include/jdbc/)
include_directories(/usr/local/Cellar/boost/1.72.0_3/include/)
add_executable(mySQL_Example main.cpp)
The following allows the source to compile.
The following is from:
https://qnaplus.com/how-to-access-mysql-database-from-c-program/
The following code works on my Slackware server, when compiled with;
gcc -o mysql_test $(mysql_config --cflags) $(mysql_config --libs) mysql_test.c
(see here for mysql_config flags)
#include <mysql.h>
#include <stdio.h>
int main()
{
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = ""; /*password is not set in this example*/
char *database = "mydb";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0))
{
printf("Failed to connect MySQL Server %s. Error: %s\n", server, mysql_error(conn));
return 0;
}
/* Execute SQL query to fetch all table names.*/
if (mysql_query(conn, "show tables"))
{
printf("Failed to execute quesry. Error: %s\n", mysql_error(conn));
return 0;
}
res = mysql_use_result(conn);
/* Output table name */
printf("MySQL Tables in mydb database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
/* free results */
mysql_free_result(res);
/* send SQL query */
if (mysql_query(conn, "select * from my_table"))
{
printf("Failed to execute quesry. Error: %s\n", mysql_error(conn));
return 0;
}
res = mysql_store_result(conn);
if (res == NULL)
{
return 0;
}
int columns = mysql_num_fields(res);
int i = 0;
printf("Entries in the table my_table:\n");
while(row = mysql_fetch_row(res))
{
for (i = 0; i < columns; i++)
{
printf("%s ", row[i] ? row[i] : "NULL");
}
printf("\n");
}
mysql_free_result(res);
mysql_close(conn);
return 1;
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.