Labels

Android (1) bash (2) boost (2) C (34) C++ (2) cheatsheet (2) CLion (6) css (3) Debian (33) DL (17) Docker (1) Dreamweaver (2) Eclipse (3) fail2ban (4) git (5) GitHub (4) Hacking (3) html (8) http (1) iOS (1) iPad (1) IRC (1) Java (30) javascript (3) Linux (164) Mac (19) Machine Learning (1) mySQL (47) Netbeans (4) Networking (1) Nexus (1) OpenVMS (6) Oracle (1) Pandas (3) php (16) Postgresql (8) Python (9) raid (1) RedHat (14) Samba (2) Slackware (45) SQL (14) svn (1) tar (1) ThinkPad (1) Virtualbox (3) Visual Basic (1) Visual Studio (1) Windows (2)

Wednesday 24 June 2020

To connect with a MySQL database with C++ including boost

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.

g++ -I /usr/local/Cellar/boost/1.72.0_3 main.cpp -I /usr/local/mysql-connector-c++-8.0.18/include/jdbc -I /usr/local/Cellar/boost/1.72.0_3/include/boost -o main.o /usr/local/mysql-connector-c++-8.0.18/lib64/libmysqlcppconn.dylib










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.