Having trouble configuring C++ MySQL API in XCode - c++

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!

Related

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

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?

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 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.

mysql.h file can't be found

i'm trying to install connection between c++ and mysql in ubuntu 12.04. i've installed mysql-client, mysql-server, libmysqlclient15-dev, libmysql++-dev. but when i try to compile the code i got the error: mysql.h there is no such file. i looked in the folders, there is mysql.h file, i can't understand why it can't find it. here is my code:
/* Simple C program that connects to MySQL Database server*/
#include <mysql.h>
#include <stdio.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
//set the password for mysql server here
char *password = "*********"; /* set me first */
char *database = "Real_flights";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
it's worked, but now i'm facing another error like :
mysql.c: In function ‘main’:
mysql.c:21: warning: incompatible implicit declaration of built-in function ‘exit’
mysql.c:27: warning: incompatible implicit declaration of built-in function ‘exit’
/tmp/ccinQBp8.o: In function `main':
mysql.c:(.text+0x3e): undefined reference to `mysql_init'
mysql.c:(.text+0x5e): undefined reference to `mysql_real_connect'
mysql.c:(.text+0x70): undefined reference to `mysql_error'
mysql.c:(.text+0xa5): undefined reference to `mysql_query'
mysql.c:(.text+0xb7): undefined reference to `mysql_error'
mysql.c:(.text+0xe7): undefined reference to `mysql_use_result'
mysql.c:(.text+0x11c): undefined reference to `mysql_fetch_row'
mysql.c:(.text+0x133): undefined reference to `mysql_free_result'
mysql.c:(.text+0x141): undefined reference to `mysql_close'
collect2: ld returned 1 exit status
The mysql.h file from the libmysqlclient-dev Ubuntu package is located at /usr/include/mysql/mysql.h.
This is not a standard search path for compilers, however /usr/include is.
You'd typically use the mysql.h header in your code like this:
#include <mysql/mysql.h>
If you don't want to specify the directory offset in your source, you can pass the -I flag to gcc (If that's what you are using) to specify an additional include search directory, and then you wouldn't need to change your existing code.
eg.
gcc -I/usr/include/mysql ...
just use
$ apt-get install libmysqlclient-dev
which will automatically pull the latest libmysqlclient18-dev
I have seen older versions of libmysqlclient-dev (like 15) puts the mysql.h in weird locations e.g. /usr/local/include etc.
otherwise, just do a
$ find /usr/ -name 'mysql.h'
and put the folder path of your mysql.h with -I flag in your make file. Not clean but will work.
For CentOS/RHEL:
yum install mysql-devel -y
this worked for me
$ gcc dbconnect.c -o dbconnect -lmysqlclient
$ ./dbconnect
-lmysqlclient is must.
and i would recommend to use following notation instead of using -I compilation flag.
#include <mysql/mysql.h>
You probably don't included the path to mysql headers, which can be found at /usr/include/mysql, on several unix systems I think. See this post, it may be helpfull.
By the way, related with the question of that guy above, about syntastic configuration. One can add the following to your ~/.vimrc:
let b:syntastic_c_cflags = '-I/usr/include/mysql'
and you can always check the wiki page of the developers on github. Enjoy!
You have to let the compiler know where the mysql.h file can be found. This can be done by giving the path to the header before compiling. In IDEs you have a setting where you can give these paths.
This link gives you more info on what options to use while compiling.
To your second problem
You need to link the libraries. The linker needs to know where the library files are which has the implementation for the mysql functions that you use.
This link gives you more info on how to link libraries.
I think you can try this gcc -I/usr/include/mysql *.c -L/usr/lib/mysql -lmysqlclient -o *
For those who are using Eclipse IDE.
After installing the full MySQL together with mysql client and mysql server and any mysql dev libraries,
You will need to tell Eclipse IDE about the following
Where to find mysql.h
Where to find libmysqlclient library
The path to search for libmysqlclient library
Here is how you go about it.
To Add mysql.h
1. GCC C Compiler -> Includes -> Include paths(-l) then click + and add path to your mysql.h In my case it was /usr/include/mysql
To add mysqlclient library and search path to where mysqlclient library see steps 3 and 4.
2. GCC C Linker -> Libraries -> Libraries(-l) then click + and add mysqlcient
3. GCC C Linker -> Libraries -> Library search path (-L) then click + and add search path to mysqlcient. In my case it was /usr/lib64/mysql because I am using a 64 bit Linux OS and a 64 bit MySQL Database.
Otherwise, if you are using a 32 bit Linux OS, you may find that it is found at /usr/lib/mysql
This worked for me
yum install mysql
It will install mysql client and then
pip install mysqlclient

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.