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.
Related
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?
Of late, I am facing a problem with a code snippet which doesn't return expected values and this is causing an application to fail.
The code is built on Redhat linux 7.1 using the following command -
g++ ace-test.cpp -I<path-to-ace-6.2-root> -L<path-to-ace-6.2-root>/ace/Linux -g -lACE
The code snippet being built is pasted below -
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "ace/MEM_Addr.h"
#include <iostream>
using namespace std;
int main(int argc, char* []) {
ACE_MEM_Addr addr ;
cout << "ACE_MEM_Addr::addr.get_host_name() " << addr.get_host_name() << endl ;
}
On execution, the code prints the following line
ACE_MEM_Addr::addr.get_host_name() **unknown**
on a system running linux which was recently patched.
On a different system which was not patched, the same code returns the correct hostname.
I am trying to figure out which patch caused this problem, but couldn't make much progress.
If someone can please extend a bit of help, would greatly appreciate it.
If required, I can share the list of patches which were applied.
Finally managed to find the culprit.
Here are the entries in /etc/nsswitch.conf:
hosts: files dns - (On the host without patches)
versus
hosts: files dns myhostname - (On the host which was patched)
Removal of the myhostname entry in the latter fixed the problem.
Associated RHEL knowledge base entry -
https://access.redhat.com/solutions/2766251
Thanks a bunch for all the help !
I am pretty new to CGI's and wanted to give them a try. Here is my situation:
My website is hosted on a linux server, I have contacted the hosting company and they said as long as I use the cgi-bin folder there is no problem. So I wrote a very short c++ program that just output some text:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello from c++ cgi";
return 0;
}
Since its a linux server (and I am running Windows) I suppose I needed to compile the source in linux so a installed a virtual machine running Ubuntu. I then compiled the source (tested it in Ubuntu) and placed the binary file in the cgi-bin folder. Then I used a simple html anchor to link to it:
T
But when I click the link on my page I get the "500 Internal Server Error" message. I have also tried to rename the binary file to "cgiTest.cgi" and update the link but still the same problem.
Anybody have any idea where my problem lies?
You forgot to output any HTTP headers (the error log entry would give you a hint).
#include <iostream>
using namespace std;
int main()
{
cout << "Content-Type: text/plain" << endl << endl;
cout << "Hello from c++ cgi";
return 0;
}
Thanks for the help, turns out I had to change the permissions and the hosting server is 32bit while I compiled on 64bit Ubuntu. I now compiled on 32bit Ubuntu and voila!, its running. Thanks to all for the help in solving this issue!
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.
I'm writing an application in C++ (using Eclipse with Linux GCC) that's supposed to interact with my MySQL server.
I've downloaded the MySQL Connector C++ a, precompiled, and copied the files into the directories (/usr/lib, /usr/include). I've referenced in in the GCC C++ Linker Section of the Project Properties in Eclipse ( "mysqlcppconn"). My code comes directly from the MySQL Reference (Hello World) except I removed error handling and the delete statements at the end (I'm not even getting there so whats the point)
#include <stdlib.h>
#include <iostream>
#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next())
{
cout << "\t... MySQL replies: ";
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
cout << res->getString(1) << endl;
}
return EXIT_SUCCESS;
}
now when I compile this, It gives me a make error 1, which the man page says means I need to recompile my packages. I've tried it, it doesn't work.
I seem to have hit dead ends everywhere. Can anyone tell me how to fix this problem? (make could really tell me what I should recompile.) Anyone have any tips/suggestions/ideas where I went wrong?
Edit - Chagned the code a bit, but the general idea is the same.
Update - If I use the terminal instead of exlipse to make, it tells me that the problem is obviously that the connector wants libstdc++.so.5 whilst I have libstdc++.so.6.
The Solution is quite simple - compile your own Connector. I did it with the 1.0.5 version of the Connector. to do it, you need to install the package via
sudo apt-get install mysql-client
In the directory of the source package you downloaded (and extracted), type
cmake .
apparently, in three files of the driver, references are made to snprintf and printf, without including the stdio.h header. I added
#include <stdio.h>
to each file and then, in the terminal, typed
make
then, I copied the Files to my lib directory
sudo cp path/to/driver/libmysqlcppconn* /usr/lib/
and everythign worked a charm.
Download and install the C++ MySQL development packages for your distribution. For ubuntu, run # sudo apt-get install libmysql++-dev.