Help me setup mysql with c++

tpfaff

2[H]4U
Joined
Jan 30, 2006
Messages
2,526
I've searched the web, followed many tutorials, and I still can't get the damn thing set up correctly. I've never used an api before and I can't figure this out. Error after error after error. I'm installing ubuntu right now and maybe that will make things easier. Any good tips? Any easy solutions? Would someone be so kind as to hold my hand through setting this up? I've tried mysql++,sqlapi++ and one other.
 
You haven't given us much to go on here...

Is MySQL installed?
Is it running?
Is MySQL++ installed?
Is your client app compiling?
Is it linking?
Is it running?
Is it connecting?
 
I remember having trouble too but ended up getting it. What exactly is happening? I might be able to remember what I did.
 
You haven't given us much to go on here...

Is MySQL installed?
Is it running?
Is MySQL++ installed?
Is your client app compiling?
Is it linking?
Is it running?
Is it connecting?

MySQL is Installed and running via godaddy. (just read the mysql++ read me and it has to be installed on my machine as well) Which one should I pick from here? http://www.mysql.com/downloads/

MySQL++ is installed but probably incorrectly. Just redownloaded the tar...
app is not compiling
I dont know if its linking
No
No
 
mysql with c++?

Are you trying to build it? Are you trying to link against it? Are you trying to write a c++ app that interfaces with it? What kind of errors are you getting compiling whatever you're compiling?

Tell us everything dude. We're not mind readers.
 
I dont really know I guess, I'm very new to this. I'm trying to write a c++ app that can connect to and query a mysql database simply.
 
When compiling you need to put a bunch of stuff in the compile string. I usually just make a shell script for it as it can get quite long.

This should allow you to compile an app that uses mysql++.

Code:
g++ -o uogpoller uogpoller.cpp -g -w -lmysqlpp -L/usr/lib64/mysql -lmysqlclient -I/usr/local/include/mysql++/ -I/usr/include/mysql

Those paths may be different on your system so some trial and error may be needed.

Post any errors that you get while compiling, or executing, if you get it to compile.
 
Forget the .tar. Use the package manager.
sudo apt-get install libmysql++-dev

Care to explain exactly what you need this for? If you just need to run queries on the database, you can use the mysql command line tool. If you don't know C++ and you don't have a good reason to use it, I'd pick another language...
 
Can you show me how to do it on windows?
Well, that explains a bit... AFAIK they don't distribute Windows binaries, so I'm guessing you haven't actually built mysql++ yet.

If you don't know what you're doing, it's probably quicker to install Ubuntu and use the package manager. But like I said, many other languages are easier to write and easier to run, so I'd forget about C++ unless you've got a good reason to be using it.
 
Ohhhhh that changes things.

I have never successfully gotten mysql++ to work in Windows. It's doable, but very complex. You need cygwin too I believe. That's all I know.

In fact, after coding lot of things in Linux, I hate coding in Windows so I usually avoid it. :p
 
I need it to interface with c++ because thats what the company I'm working for is using. I don't know why. Anyways, I'll try it out in ubuntu, this is just a nightmare. I got qt for windows and its really easy to use SQL but not mySQL
 
When compiling you need to put a bunch of stuff in the compile string. I usually just make a shell script for it as it can get quite long.

This should allow you to compile an app that uses mysql++.

Code:
g++ -o uogpoller uogpoller.cpp -g -w -lmysqlpp -L/usr/lib64/mysql -lmysqlclient -I/usr/local/include/mysql++/ -I/usr/include/mysql

Those paths may be different on your system so some trial and error may be needed.

Post any errors that you get while compiling, or executing, if you get it to compile.

Ok so it compiled..I think? I've only ever compiled stuff with gcc by g++ *.h *.cpp and then running it with ./a.out

Entering the command you gave me gave no errors in the terminal, but when I ./a.out it doesnt exist.

Edit: Never mind im retarded. Got it to run but you could explain to me what those commands do? So where I'm at right now it compiled while including mysql.h, going to try to connect to the database now! :)
 
T the -lmysqlpp and -lmysqlclient tells it to use the mysqlp++ lib but TBH I'm not 100% sure of the others, I think they just tell the compiler where the libs are located. I think this was in the mysql++ docs, or some tutorial I found, I forget.
 
Ok thanks! I'm having some trouble again, according to a tutorial I found...

Connection con("mysql_cpp_data");
// The full format for the Connection constructor is
// Connection(cchar *db, cchar *host="",
// cchar *user="", cchar *passwd="")

What the hell is a cchar? using strings gives me a type conversion error, cant convert string to cchar, as expected when viewing the format for the constructor!
 
Got it to run but you could explain to me what those commands do?
From memory,
-o (filename) = output file
-g = include debug symbols
-w = warnings off
-I(path) = tell your compiler where to look for headers
-L(path) = tell your linker where to look for libraries
-labc = link to libabc.a or libabc.so (in the path specified above)

The typedef for cchar will be in a header somewhere. But I'm guessing it's pretty much a char. You probably want string::c_str to get a char* out of your C++ string object.

And FWIW, if you're not comfortable with C++, I'd just do it in C and use the standard MySQL client library. C is far easier to pick up, and your company's C++ code can still call it.
 
Thanks, I'm actually more comfortable in c++. I'll look into the typedef. If it's a char though that doesnt make much sense. Some of the parameters for that function are username and password, which are definitely strings :confused:

Edit: Found out it stands for constant char, but still....why is it a char?
 
Last edited:
The parameters are cchar pointers.

C strings are just pointers to the first char in a null-terminated array. string::c_str returns a char*.
 
Last edited:
Forget the .tar. Use the package manager.
sudo apt-get install libmysql++-dev

Is this all I need to do to install it?

I tried to compile using g++ -o test test.cpp -g -w -lmysqlpp -L/usr/lib64/mysql -lmysqlclient -I/usr/local/include/mysql++/ -I/usr/include/mysql

and got this error

test.cpp:1:21: fatal error: mysql++.h: No such file or directory
compilation terminated.

After browsing to usr/local/include/ there is nothing in there. What more do I need to do to install it then?

Edit: Nevermind found the headers in a different directory!
 
Last edited:
Back
Top