tags:

views:

597

answers:

8

Does anyone know of a simple way for a C++ program to communicate directly with a MySQL database? I have looked at MySQL++ and found it to be very confusing. If anyone knows of a very simple way of doing this, please let me know.

Thanks

P.S. I am developing on a Windows machine. PHP and MySQL web web application setup. C++ setup to talk to the serial port. Thus why I need a way for C++ to talk to the MySQL database directly.

+1  A: 

Use the C API:

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

Jamie Love
I've been using that, and it's moderately groady to deal with. Avoid if possible, imo.
Paul Nathan
I've found it very easy to use actually. But I'd recommend writing a wrapper that retries automatically on lock timeouts or connection fails.
Mark Baker
+6  A: 

The C MySQL API is just like using the PHP MySQL extension and so should be pretty familiar. If you are comfortable with C, I'd recommend that (and you don't mind mixing C in your C++).

Stephen Caldwell
A: 

MySQL has a beta release of MySQL Connector C++ that can be found Here. It's a connector written in C++ but modeled after Java's JDBC connectors. If you've used JDBC at all, this will feel almost identical. It's also cross platform and can be compiled on Linux, Windows, and Mac OS.

Grant Limberg
+6  A: 

There are quite a few database API wrappers but my favourite - and the one I recommend - is the SOCI library. Much nicer syntax than using the raw C API.

The 'motivating' example from the SOCI website:

int id = ...;
string name;
int salary;

sql << "select name, salary from persons where id = " << id, into(name), into(salary);
MattyT
That's really quite nice.
Stephen Caldwell
It gets better; have a look at the rowset syntax including the iterators. Also, the way it can use boost::optional and boost::tuple is really slick!
MattyT
A: 

I have used MySQL++ with great success. I found it to be everything I was looking for in a MysQL database client library. The examples they provide are great, see here for a simple one.

If you know C++, it is much easier to use than the C library. It works great with Windows.

Tim Weiler
A: 

Are you sure that the C++ program needs to interact directly with the MySQL database?

I would suggest having the C++ program communicate with the web server using simple http requests, and let PHP handle the database. Data can be passed to a server-side PHP script via arguments, and data can be returned in XML format.

Writing: your.server.com/add_data.php?table="info"&data="0xFCBD..."

Reading: your.server.com/get_data.php?table="info"

e.James
I'm still not sure why this keeps getting downvoted. I've been in a similar situation before, and eventually chose to use http messaging as I described in this answer.
e.James
This isn't a bad workaround - if you're not a stickler for performance.
anbanm
A: 

Use SQLAPI++ - it does SQL Server and more.

http://www.sqlapi.com/

Rob
A: 

MySQL Connector/C++ is a C++ implementation of JDBC 4.0

The reference customers who use MySQL Connector/C++ are: - OpenOffice - MySQL Workbench

Learn more: http://forums.mysql.com/read.php?167,221298