Connecting mysql to C++ in VS Code, compiled succesfully but not running - c++

I'm new to databases, so I wanted to make a program to perform simple queries in mysql with C++ in VS Code, in Windows 10. Last time I had problems with linking the library, and now it seems like I managed to fixed them. I have the following code taken from another source by adding my system configurations:
#include <iostream>
#include <windows.h>
#include "C:/Program Files/MySQL/MySQL Server 8.0/include/mysql.h"
int main(){
MYSQL* conn;
conn = mysql_init(0);
conn = mysql_real_connect(conn, "localhost", "root", "password", "project", 0, NULL, 0);
if(conn){
std::cout << "Connected" << std::endl;
} else {
std::cout << "Not connected" << std::endl;
}
}
When I compile it with the command g++ main.cpp -Wall -Werror -I "C:/Program Files/MySQL/MySQL Server 8.0/include" -L "C:/Program Files/MySQL/MySQL Server 8.0/lib" -lmysql, it compiles without reporting any errors. However, if I try to run it, the program simply terminates. I don't understand what problems could be. I suspect the problem might be with mysql connector, but as I said I'm new to it, so I still have doubts. So I would really appreciate it if you could help me out how I can proceed further.
I looked for similar questions, and they helped me only for linking to the library.

Fixed it. Just had to add libmysql.dll in a folder with the main.cpp file. Thank you all for your advices.

mysql_init can return NULL:
An initialized MYSQL* handler. NULL if there was insufficient memory to allocate a new object.
Wouldn't be safer to check its value before second call?

Related

MySql C++ Connector crashes project on startup [duplicate]

It's in the title, I want to create a program in C which connects to my MySQL database which is hosted locally with MAMP, I use as IDE CLion and I'm on Windows. I also use the MySQL API that I installed in my MinGW directory
So to do this I use this code:
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <MYSQL/mysql.h>
int main(int argc, char **argv)
{
printf("\nhello");
MYSQL *con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
printf("\ntest");
if (mysql_real_connect(con, "localhost", "root", "root",
"perfect-concierge", 3307, NULL, 0) == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
printf("\ntest2");
if (mysql_query(con, "CREATE DATABASE testdb"))
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
printf("\ntest3");
mysql_close(con);
exit(0);
}
"Tests" are there to see where there would be an error. And I use this CMakeFile.txt :
cmake_minimum_required(VERSION 3.12)
project(Test-MySQL C)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_FLAGS "-Wall")
add_library(libmysql SHARED IMPORTED)
set_target_properties(
libmysql
PROPERTIES LINKER_LANGUAGE C
IMPORTED_IMPLIB "C:/MinGW/lib/libmysqlclient.a")
link_directories("C:/MinGW/lib")
add_executable(Test-MySQL main.c)
target_link_libraries(Test-MySQL libmysql)
(PS : It is not me who made this CMakeFile but the one who gave me this example of connection to a MySQL database)
But when I execute this code the program compiles well, it runs then I get this error :
Process finished with exit code -1073741515 (0xC0000135)
Without anything else. I tried to just change the main function and just keep printf("\nhello"); and the program worked correctly, but just add MYSQL *con = mysql_init(NULL); send me this error again
Thank you in advance for the time you will take to help me.
The error
Process finished with exit code -1073741515 (0xC0000135)
means that some .dll cannot be found when the executable is run (at runtime).
On Windows you need either
Have directory with .dll (C:/MinGW/lib in your case) to be in your PATH variable.
Have .dll itself near the executable (at the same directory). See that question for the way how to achieve that in CMake.
Note, that link_directories (and similar) affects only on search the library during the linking stage and doesn't help at runtime.
MAMP is Macintosh, Apache, MySQL, Perl/Python/PHP. Which is the Apache as web server, MySQL as database server and Perl/Python/PHP as a module for the Web Server. Moreover, the important thing, MAMP is compatible in MacOS/Macintosh.
As your description, you are using Windows as the operating system, and C as your program language. So, for sure you can not use MAMP as your server.
Alternatively, You need to find any server that compatible for your Windows OS and your C as program language.
Hopefully it helps!

How can I deal with dll and lib files for connecting MariaDB to C++ Application?

I am trying to work on the project which is to connect MariaDB with C++ application.
I referred to the URL: https://mariadb.com/docs/clients/connector-cpp/#installing-mariadb-connector-c-via-msi-windows.
This URL is quite a good source to connect MariaDB with C++. However, it doesn't describe how to deal with the lib file and dll file.
When I installed the MariaDB connector/C++ via MSI, it gave me several files: conncpp.hpp, mariadbcpp.dll, mariadbcpp.lib, etc.
I tried to include mariadb/conncpp.hpp by setting the path C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\include and I did same with the lib file.
Also, I went to the properties and set the linkers for the lib file.
Here is the code that I am planning to execute:
// Includes
#include <iostream>
#include <mariadb/conncpp.hpp>
// Main Process
int main(int argc, char** argv)
{
try
{
// Instantiate Driver
sql::Driver* driver = sql::mariadb::get_driver_instance();
// Configure Connection
// The URL or TCP connection string format is
// ``jdbc:mariadb://host:port/database``.
sql::SQLString url("jdbc:mariadb://192.0.2.1:3306/test");
// Use a properties map for the user name and password
sql::Properties properties({
{"user", "db_user"},
{"password", "db_user_password"}
});
// Establish Connection
// Use a smart pointer for extra safety
std::unique_ptr<sql::Connection> conn(driver->connect(url, properties));
// Use Connection
// ...
// Close Connection
conn->close();
}
// Catch Exceptions
catch (sql::SQLException& e)
{
std::cerr << "Error Connecting to MariaDB Platform: "
<< e.what() << std::endl;
// Exit (Failed)
return 1;
}
// Exit (Success)
return 0;
}
But whenever I compile and execute the code, it says Unhandled exception(0x00007FF918058D25(mariadbcpp.dll), MariaDB_Connection.exe): 0xC0000005:Access violation reading location 0xFFFFFFFFFFFFFFFF at the line of
std::unique_ptr<sql::Connection> conn(driver->connect(url, properties));.
Could you tell me how to solve this problem?
You've hit the deficiency in the C++ connector API. Or call it a bug. It's Windows specific. Debug and release versions of STL objects may have different layout.
Switching build to release configuration should help. But some issues are still possible depending on VS version used. As a workaround you could also try to use other connect method, i.e.
std::unique_ptr<sql::Connection> conn(driver->connect(url, "db_user", "db_user_password"));
since it doesn't rely on Properties map, that causes the issue here.

How to integrate MySQL with codeblocks IDE

I have been searching solution for this problem whole day.Any help would make me grateful. I am using Code::blocks IDE 16.01. I want to connect from my IDE to my MySQL database server. I have tried a lot of options but yet to succeed. I have my MYSQL INSTALLER setup already and have downloaded the mysql connector and mysql server. I am gonna show you the code and the way i have already tried.
This is my simple code
#include <iostream>
#include <windows.h>
#include <mysql.h>
using namespace std;
int main()
{
MYSQL* conn;
conn = mysql_init(NULL);
if (mysql_real_connect(conn,"localhost","root","","test",0,NULL,0) !=0)
{
cout << "Succesfully Connected to MySQL database xxxx" << endl;
}
mysql_close(conn);
return 0;
}
I have my MySQL setup in C drive and I have linked that as below
After doing all the work I have been shown the following errors
Please someone help me. Thanks in advance. Please feel free to ask If you guyz need anything to know.
In third step of your CodeBlocks setup you must specify the lib path (and not the include path), as below:
C:\Program Files\MySQL\MySQL Server 8.0\lib .
Then rebuilds your application.

Having trouble configuring C++ MySQL API in XCode

I am trying to configure C++ MySQL API in Xcode. I followed the instruction that the teacher provide us, first downloaded and installed the MySQL community server and Connector for C. I also edit Project setting:
Search Paths - Header Search Paths
/usr/local/mysql/include
Search Paths - Library Search Paths
/usr/local/mysql/lib
Linking - Other Linker Flags
-lmysqlclient
-lm
-lz
However, when I ran the code:
#include <mysql.h>
#include <iostream>
using namespace std;
int main (int argc, const char * argv[])
{
MYSQL *connection, mysql;
mysql_init(&mysql);
connection = mysql_real_connect(&mysql, "localhost", "root", "", "project3-nudb", 0, 0, 0);
if (connection == NULL)
{
//unable to connect
printf("Oh Noes!\n");
}
else
{
printf("You are now connected. Welcome!\n");
}
}
It kept showing "Oh Noes!\n" which indicate unable to connect. In the instruction slide, it also states:
In the terminal: (this step is important or the project can not find libmysqlclient.18.dylib)
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib
/usr/lib/libmysqlclient.18.dylib
I have tried the first line and don't quite know whats the second line mean. Since I look up the path /usr/lib/, there's no such file in the directory. I also tried to copy /usr/local/mysql/lib/libmysqlclient.18.dylib to /usr/lib/ but still not working.
Could someone tell me what step I got wrong? Thanks!

link libmysql.lib with gcc or dev c++ in windows

i've been trying to link mysql with c++ below is the code for ref
the file called sqlfunction.cpp has following code which helps connect mysql
#include <mysql.h>
#include "rlmodbusclient.h"
#include "modbusdaemon.h"
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
bool Opendb(char *pc,char *user, char *pass, char *db)
{
conn = mysql_init(NULL);
// Connect to database
if (!mysql_real_connect(conn, pc,
user, pass, db, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
} else return 1;
}
now if above called from the program files like this
char pc[10]="localhost",user[5]="root",pass[8]="pass",db[6]="database";
ret = Opendb(pc,user,pass,db);
printf("opendb_buttonevent = %d\n",ret);
this is all good but the only thing is program is not compiling at all
the error is
undefined reference to mysql_init#4'
i found one solution for this which is
http://www.openwebspider.org/documentation/how-to-link-libmysqllib-with-dev-c-or-gcc-under-windows/
this link suggest to run reimp.exe with libmysql.lib
i tried to run this but this doesnt make any sense please if some one has done above then explain how to use reimp to solve this issue..
thanks
just like to add command which i'm using to run reimp
C:\Documents and Settings\XPMUser\Desktop\mingwutils\bin>reimp.exe "C:\Program F
iles\MySQL\MySQL Server 5.5\lib\libmysql.lib"
reimp.exe: dlltool: No such file or directory
as can be seen it says dlltool: no such file but i found that dlltool is part of
C:\MinGW\bin directory... which doesnt make any sense?????
The general problem is that the libmysql.lib library is in the Microsoft-specific lib format, which mingw cannot link against. It seems that reimp.exe can convert such libraries and needs dlltool to do so. It probably couldn't find it because it is not in your PATH. Try
set PATH=%PATH%;C:\MinGW\bin
reimp.exe "C:\Program Files\MySQL\MySQL Server 5.5\lib\libmysql.lib"
Afterwards, you should gain a libmysql.a file, which must be moved somewhere the mingw linker can find it, e.g. your project directory.