Add mysql test, update gitignore.
This commit is contained in:
parent
c619a88a70
commit
124bfb668e
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,6 @@
|
||||
# New
|
||||
build.tar
|
||||
|
||||
# ---> C
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -3,6 +3,7 @@
|
||||
"math_functions.h": "c",
|
||||
"pointer_test.h": "c",
|
||||
"struct_test.h": "c",
|
||||
"stdio.h": "c"
|
||||
"stdio.h": "c",
|
||||
"stdlib.h": "c"
|
||||
}
|
||||
}
|
@ -1,27 +1,55 @@
|
||||
cmake_minimum_required(VERSION 3.5.0)
|
||||
# cmake_minimum_required(VERSION 3.5.0)
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(Test_Project VERSION 0.1.0 LANGUAGES C)
|
||||
|
||||
# Hmm, this format could be useful.
|
||||
# if(WIN32)
|
||||
# endif()
|
||||
|
||||
SET(MARIADB_INCLUDE_DIR "C:/Program Files/MariaDB/MariaDB Connector C 64-bit/include")
|
||||
SET(MARIADB_LIBRARY_DIR "C:/Program Files/MariaDB/MariaDB Connector C 64-bit/lib")
|
||||
|
||||
SET(BINARY_NAME "Test_Project")
|
||||
|
||||
# MySql test
|
||||
# https://dev.mysql.com/blog-archive/the-client-library-part-4-how-to-write-a-simple-mysql-client-in-c-using-cmake-and-pkg-config/
|
||||
|
||||
# https://stackoverflow.com/questions/72786137/how-to-compile-c-with-cmake-and-l-usr-include-mariadb-mysql-lmariadbclient
|
||||
|
||||
# Add more c files in here
|
||||
# add_executable(Test_Project
|
||||
# add_executable(${BINARY_NAME}
|
||||
# main.c
|
||||
# hello.c
|
||||
# math_functions.c
|
||||
# struct_test.c
|
||||
# )
|
||||
|
||||
add_executable(Test_Project
|
||||
add_executable(${BINARY_NAME}
|
||||
src/main.c
|
||||
src/hello.c
|
||||
src/math_functions.c
|
||||
src/struct_test.c
|
||||
src/pointer_test.c
|
||||
src/read_file.c
|
||||
src/mysql_test.c
|
||||
)
|
||||
|
||||
# https://discourse.cmake.org/t/how-to-properly-include-header-files/4762
|
||||
|
||||
# Add header files in here, not sure if I did this right.
|
||||
target_include_directories(Test_Project
|
||||
PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
target_include_directories(${BINARY_NAME}
|
||||
PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
"${MARIADB_INCLUDE_DIR}"
|
||||
# "${LIBMYSQLCLIENT_INCLUDE_DIRS}"
|
||||
)
|
||||
|
||||
)
|
||||
# https://stackoverflow.com/questions/2601798/adding-compiled-libraries-and-include-files-to-a-cmake-project
|
||||
target_link_libraries(${BINARY_NAME}
|
||||
"${MARIADB_LIBRARY_DIR}/libmariadb.lib"
|
||||
"${MARIADB_LIBRARY_DIR}/mariadbclient.lib"
|
||||
)
|
||||
|
||||
# add_library(Test_Project
|
||||
# PUBLIC "${MARIADB_LIBRARY_DIR}/libmariadb.lib"
|
||||
# "${MARIADB_LIBRARY_DIR}/mariadbclient.lib"
|
||||
# )
|
15
src/main.c
15
src/main.c
@ -1,4 +1,4 @@
|
||||
#include <stdio.h>
|
||||
#include <stdio.h>
|
||||
// #include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
@ -9,6 +9,8 @@
|
||||
#include "pointer_test.h"
|
||||
#include "read_file.h"
|
||||
|
||||
#include "mysql_test.h"
|
||||
|
||||
|
||||
// Test
|
||||
// https://stackoverflow.com/questions/8666378/detect-windows-or-linux-in-c-c
|
||||
@ -39,6 +41,11 @@
|
||||
// 7. Run this CMake command to generate build files: "cmake -G "MinGW Makefiles" .."
|
||||
// 8. To build Run "make"
|
||||
|
||||
// I switched to msys2 and used this guide for the toolkit:
|
||||
// https://www.devdungeon.com/content/install-gcc-compiler-windows-msys2-cc
|
||||
|
||||
// Installed mysql in mingw64 using msys2:
|
||||
// pacman -S mingw-w64-x86_64-libmariadbclient
|
||||
|
||||
// TODO Look into getting current directory in Windows and Linux
|
||||
// https://stackoverflow.com/questions/27585930/get-the-current-working-directory-in-c-on-windows
|
||||
@ -83,6 +90,8 @@ void array_test() {
|
||||
|
||||
}
|
||||
|
||||
// Removed from path in windows: C:\mingw64\bin
|
||||
|
||||
// https://www.tutorialspoint.com/cprogramming/c_ternary_operator.htm
|
||||
void check_even_odd(int num) {
|
||||
(num %2 == 0) ? printf("%d is even\n", num) : printf("%d is odd\n", num);
|
||||
@ -102,7 +111,7 @@ int main() {
|
||||
// check_even_odd(1);
|
||||
|
||||
// print_struct();
|
||||
read_file("test.txt");
|
||||
// read_file("test.txt");
|
||||
|
||||
|
||||
// This doesn't work
|
||||
@ -121,6 +130,8 @@ int main() {
|
||||
int* i_ptr = &i;
|
||||
// pointer_madness(i_ptr);
|
||||
|
||||
connect_mysql();
|
||||
|
||||
#ifdef _FLIP_BOOLS
|
||||
bool testb = false;
|
||||
if (testb) {
|
||||
|
86
src/mysql_test.c
Normal file
86
src/mysql_test.c
Normal file
@ -0,0 +1,86 @@
|
||||
// I had to install MariaDB Connector from here and add the path to the CMake include
|
||||
// MariaDB Connector: https://mariadb.com/downloads/connectors/
|
||||
// Include path : C:/Program Files/MariaDB/MariaDB Connector C 64-bit/include
|
||||
|
||||
// #include <mysql/mysql.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <mysql.h>
|
||||
|
||||
// TODO Fix this to work.
|
||||
|
||||
// https://stackoverflow.com/questions/44860756/how-to-connect-and-insert-record-to-mysql-using-c-language
|
||||
void connect_mysql() {
|
||||
MYSQL *conn;
|
||||
// MYSQL *conn;
|
||||
|
||||
mysql_init(conn);
|
||||
|
||||
// TODO Move these into a file
|
||||
const char* mysql_host = "localhost";
|
||||
const char* mysql_username = "";
|
||||
|
||||
const char* mysql_password = "";
|
||||
const char* mysql_database = "";
|
||||
unsigned int mysql_port = 3306;
|
||||
|
||||
const char* mysql_table_insert = "INSERT INTO table_1 VALUES ('Hello World');";
|
||||
// const char* mysql_table_select = "SELECT * FROM ";
|
||||
const char* mysql_table_show = "SHOW TABLES";
|
||||
|
||||
|
||||
if((conn = mysql_init(NULL)) == NULL) {
|
||||
fprintf(stderr, "Could not init DB\n");
|
||||
// return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Set mysql options
|
||||
// mysql_options(conn, );
|
||||
|
||||
// Gives an error
|
||||
// if (mysql_real_connect(conn, mysql_host, mysql_username, mysql_password, mysql_database, mysql_port, NULL, 0) == NULL) {
|
||||
if (!mysql_real_connect(conn,
|
||||
mysql_host,
|
||||
mysql_username,
|
||||
mysql_password,
|
||||
mysql_database,
|
||||
mysql_port,
|
||||
NULL,
|
||||
0)){
|
||||
// 0) == NULL) {
|
||||
// CLIENT_FOUND_ROWS) == NULL) {
|
||||
fprintf(stderr, "DB Connection Error\n");
|
||||
// return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Gives an error
|
||||
// if (mysql_query(conn, mysql_table_insert) != 0) {
|
||||
if (mysql_query(conn, mysql_table_show) != 0) {
|
||||
fprintf(stderr, "Query failure");
|
||||
// return EXIT_FAILURE;
|
||||
} else {
|
||||
// https://github.com/hholzgra/connector-c-examples/blob/master/mysql_fetch_row.c#L61
|
||||
MYSQL_RES *result = mysql_store_result(conn);
|
||||
|
||||
if(!result) {
|
||||
printf("Couldnt get results set %s\n", mysql_error(conn));
|
||||
} else {
|
||||
MYSQL_ROW row;
|
||||
int i;
|
||||
unsigned int num_field = mysql_num_fields(result);
|
||||
|
||||
while((row = mysql_fetch_row(result))) {
|
||||
for (i = 0; i < num_field; i++) {
|
||||
printf("%s ", row[i]);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
mysql_free_result(result);
|
||||
}
|
||||
}
|
||||
|
||||
mysql_close(conn);
|
||||
// printf("Inserted %s into the db", mysql_table_insert);
|
||||
// return EXIT_SUCCESS;
|
||||
}
|
1
src/mysql_test.h
Normal file
1
src/mysql_test.h
Normal file
@ -0,0 +1 @@
|
||||
void connect_mysql();
|
Loading…
Reference in New Issue
Block a user