Sign in to follow this  
RyanZec

Connecting to MySQL database with C++

Recommended Posts

RyanZec    124
I am creating a project management system and the intiial version is going to have a PHP based interface. What would be nice is to also offer a C++ interface however i can't seem to find a libraries to help with connecting and queries a MySQL database. anyone know of any libraries of tutorials on how to do this?

Share this post


Link to post
Share on other sites
Antheus    2409
MySQL C API.

http://dev.mysql.com/doc/refman/5.0/en/c.html

Yes, it's C API.

There's various C++ ports, wrappers, etc., but I found them all either: Useless, abandoned, obsolete, too bulky or not-offering-enough.

So in the end, I ended up using the MySQL provided one, which is always up to date and regularly maintained, and can be easily integrated into C++. This way I also have a nice choice between low ( by library ) and high-level (my API) level access to the database.

The only major hurdle with this library is thread safety, but that one can easily be solved.

Share this post


Link to post
Share on other sites
eq    654
Quote:
The only major hurdle with this library is thread safety, but that one can easily be solved.

Out of curiosity, how?
I've got my own wrapper around this lib, that amongst other things makes it thread safe.
Is there a better approach?

Share this post


Link to post
Share on other sites
Antheus    2409
Quote:
Original post by eq
Quote:
The only major hurdle with this library is thread safety, but that one can easily be solved.

Out of curiosity, how?
I've got my own wrapper around this lib, that amongst other things makes it thread safe.
Is there a better approach?


No, there isn't.

The C API and some pre-compiled libs as provided might not be thread-safe.

So in order to use it, you need to first build thread safe libraries, and then use them in thread safe manner.

For example, a connection, while thread-safe by implementation, may not be used cuncurrently by several threads.

This comes from the fact that from logical perspective, concurrently using a connection doesn't make sense. Creating a query, filling the buffers, sending it, waiting for response, and processing the response needs to be done in order, or the results are undefined.

The design of the API simply isn't concurrent in any way, although the implementation itself is.

A true concurrent design would use some different aproach, callbacks, or similar. But current implementation requires you to ensure that each connection executes only one query at a time.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this