简体   繁体   中英

c++ and MySQL connection

I'm totally new to c++ and I havn't done that much so far. Right now, I need to setup a DB-connection to a mysql -server, to retrieve some data.

So I found this question MySQL C++ Connector getting a string with SELECT query . I tried to setup everything like it is explained in the question, but I ended up with having several errors.

My code:

// important part for the mysql-stuff
#include <mysql/mysql.h>


// Basic setup for the DB
static char const *opt_host_name = "localhost"; /* HOST */
static char const *opt_user_name = "root"; /* USERNAME */
static char const *opt_password = "mypass"; /* PASSWORD */
static unsigned int opt_port_num = 3306; /* PORT */
static char const *opt_socket_name = NULL; /* SOCKET NAME, DO NOT CHANGE */
static char const *opt_db_name = "mydb"; /* DATABASE NAME */
static unsigned int opt_flags = 0; /* CONNECTION FLAGS, DO NOT CHANGE */

And here is the method, where I am trying to get an output:

int main(void) {
    try {

        // setup the connection
        MYSQL *conn; /* pointer to connection handler */
        MYSQL_RES *res; /* holds the result set */
        MYSQL_ROW row;

        /* INITIALIZE CONNECTION HANDLER, DO NOT CHANGE */
        conn = mysql_init(NULL);

        /* THIS CONNECTS TO SERVER, DO NOT CHANGE ANYTHING HERE */
        mysql_real_connect(conn, opt_host_name, opt_user_name, opt_password,
            opt_db_name, opt_port_num, opt_socket_name, opt_flags);

        /* show tables in the database (test for errors also) */
        mysql_query(conn, "SELECT * FROM plates");
        res = mysql_store_result(conn);

        // get the number of the columns
        int num_fields = mysql_num_fields(res);

        while ((row = mysql_fetch_row(res))) {
            // Print all columns
            for (int i = 0; i < num_fields; i++) {
                // Make sure row[i] is valid!

                cout << row[i];
            }
        }
    }
}

After calling make in the specific folder, where the file is located, it prints this:

undefined reference to mysql_init

undefined reference to mysql_real_connect

... and every function listed in that code.

This here is the Makefile

SAMPLES = cpd01.out

.PHONY: all
all: $(SAMPLES)

.PHONY: clean
clean:
    rm -f $(SAMPLES)

%.out : %.cpp
    g++ -g3 -o $@ $< -DLINUX -I/usr/include/gx -ldl -DGX_NAMESPACES

I didn't make that Makefile, and as mentioned above, I have no idea what PHONY (or else) stand for. Will have to dig a bit deeper, i guess.

If I understand PaulMcKenzie correclty, I have to add something here, right?

MySQL comes with a script you can run that tells you which libraries to link to: mysql_config --libs .

On my system it gives:

$ mysql_config --libs
-L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl

You need to add those flags to the link command (or the compiler command if you link all at the same time).

If you are using GNU Make you should be able to do this:

SAMPLES = cpd01.out

MYSQL_LIBS = $(shell mysql_config --libs)

.PHONY: all
all: $(SAMPLES)

.PHONY: clean
clean:
    rm -f $(SAMPLES)

%.out : %.cpp
    g++ -g3 -o $@ $< -DLINUX -I/usr/include/gx -ldl -DGX_NAMESPACES $(MYSQL_LIBS)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM