Hi,

I was wondering if it is possible to connect a C++ program to a MySQL database on Linux Fedroa Core 5 os. It would be great if I could create a program in C++ that allows me to create a completely new database in MySQL, edit, modify, delete and update records and tables within it. But can't find much help on the net. Can any one please lend me some of the necessary knowledge. Thanks.

Dani AI

Generated

Yes — C++ programs can talk to MySQL on Fedora (and other Linuxes). Common, practical routes are: the low-level MySQL C client library (libmysqlclient / C API), the official MySQL Connector/C++ (modern C++ API), the third‑party mysql++ wrapper (as pointed out), or an ODBC stack (unixODBC + MySQL ODBC / MyODBC, mentioned by and ). Connector/C++ and the C client are the most straightforward if writing native C++ code; ODBC is useful for portability or legacy apps. (dev.mysql.com)

For a beginner on Fedora, the simplest workflow is: install the distribution packages (server + development headers) or the connector binary RPMs, then compile against the installed headers/libraries. The MySQL client side provides the mysql_config helper to print the correct compiler and linker flags (--cflags / --libs) so build commands do not need hardcoded paths. If using Connector/C++ from CMake, prefer find_package(mysql-concpp) to get correct include/link settings. (dev.mysql.com)

If an SRPM rebuild fails because the package uses legacy spec tags, that’s a common, fixable RPM issue: older spec files sometimes use a Copyright: tag that modern rpmbuild rejects — edit the .spec inside the SRPM and replace the tag with License: (and a suitable license string), then rebuild; otherwise use the provided binary RPMs to avoid rebuilding. This explains the build errors reported earlier in this thread. (atmarkit.itmedia.co.jp)

Minimal Connector/C++ sketch and a simple compile note:

#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/statement.h>

int main() {
    sql::Driver *drv = sql::mysql::get_mysql_driver_instance();
    std::unique_ptr<sql::Connection> con(drv->connect("tcp://127.0.0.1:3306","user","pw"));
    con->setSchema("testdb");
    std::unique_ptr<sql::Statement> stmt(con->createStatement());
    stmt->execute("CREATE TABLE IF NOT EXISTS t(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50))");
    return 0;
}

Linking hint: either let CMake handle connector targets (mysql::concpp) or compile manually and link the connector library (example linker: -lmysqlcppconn) or the C client (-lmysqlclient); mysql_config helps for the latter. Also remember to set runtime library paths (LD_LIBRARY_PATH or ld configuration) if libraries are installed in nonstandard locations. (dev.mysql.com)

Summary: pick Connector/C++ for a modern C++ API, libmysqlclient for low-level control, or ODBC for cross-platform/legacy support; prefer distro or connector binaries on Fedora and edit the spec only if rebuilding an old SRPM is necessary.

Recommended Answers

All 9 Replies

Something like this ?

Something like this ?

Hi,

Thanks a lot for the reply, it seems that there is a need to download the C++ API for MySQL in order to make a connection between the two, but what actaully I am after is a simple piece of code that demonstrates the connection between C++ and MySQL. I would also like to have the basic guidelines on how to install the C++ API from the url you have kindly given to me. I would also go on trying to do it on my own, but there is a strong possibility of a failure underlying the fact that I am quite new to Linux and C++ both. More help would be sincerely appreciated. Cheers.

Hi,

I was wondering if it is possible to connect a C++ program to a MySQL database on Linux Fedroa Core 5 os. It would be great if I could create a program in C++ that allows me to create a completely new database in MySQL, edit, modify, delete and update records and tables within it. But can't find much help on the net. Can any one please lend me some of the necessary knowledge. Thanks.

hi,
I have done a similar application in windows platform.
there i have used an ODBC Driver to connect,even u can connect Mysql server directly.
u create databases,execute queries also.
bye,
sree

hi,
I have done a similar application in windows platform.
there i have used an ODBC Driver to connect,even u can connect Mysql server directly.
u create databases,execute queries also.
bye,
sree

Thanks for the help, but I am using Fedora core 5 32+ bit as my os and I would like to ask again weather I need to download the C++ API to make it all work from the following url:

http://tangentsoft.net/mysql++/

But if I do not install the above API and try to connect to a MySQL database with a C++ program is it going to work? I am a bit confused and lost here, need some more guidance. Thanks,

I guess you will have to put in a bit more effort and try it on your own. Download the documentation related to the API and do some experimentation....

I guess you will have to put in a bit more effort and try it on your own. Download the documentation related to the API and do some experimentation....

Hi,

Thanks, I have downloaded the source rpm to my desktop.

Here they are on the page (http://tangentsoft.net/mysql++/) under a big heading called "Latest Stable Version" and they are the second link saying

"mysql++-2.2.0-1.src.rpm (2.6 MB, 2007.01.23) — Source RPM, for those that need to build their own binary RPMs. To do this, run this command as root:

# rpmbuild --rebuild /wherever/it/is/mysql++-2.2.0-1.src.rpm"

and then tried to build it with the following command in a terminal:

# rpmbuild --rebuild /wherever/it/is/mysql++-2.2.0-1.src.rpm

but some error messages showed up. Here they are:

warning: user tangent does not exist - using root
warning: group tangent does not exist - using root
error: Legacy syntax is unsupported: copyright
error: line 5: Unknown tag: Copyright: LGPL


This is what the terminal windows looks like:


[root@localhost Desktop]# rpmbuild --rebuild mysql++-2.2.0-1.src.rpm
Installing mysql++-2.2.0-1.src.rpm
warning: user tangent does not exist - using root
warning: group tangent does not exist - using root
error: Legacy syntax is unsupported: copyright
error: line 5: Unknown tag: Copyright: LGPL
[root@localhost Desktop]#

Can you help me with these errors please. Can't figure out any thing here at all.

Why do you want to build from source code when pre built binaries are readily available on the site?

I haven't personally used this API but you can try downloading the binary files under "Linux Binary RPM's" and trying them out. (they even have some examples).

If that doesn't work, you can try out this API.

Why do you want to build from source code when pre built binaries are readily available on the site?

I haven't personally used this API but you can try downloading the binary files under "Linux Binary RPM's" and trying them out. (they even have some examples).

If that doesn't work, you can try out this API.

I think you are right I have made a mistake here. I will try the 4 binaries under "Linux Binary RPM's" and let you know as what it happening. Thanks your help is crucial.

libmyodbc

(a simple odbc for linux and mysql)

would be great if we had a wine odbc, I'd love to get some of my odbc reliant dbase apps runnning via wine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.