LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   MySQL client (https://www.linuxquestions.org/questions/programming-9/mysql-client-273366/)

zaichik 01-03-2005 01:42 PM

MySQL client
 
Hi all,

I am taking some steps into programming with the MySQL C APIs for an upcoming project. I have this code, mostly working from the MySQL documentation:

Code:


#include <stdio.h>
#include <mysql/mysql.h>

int main ( int argc, char **argv ) {

        MYSQL *conn;
        MYSQL_RES *res;
        MYSQL_ROW row;

        char *server = "localhost";
        char *user = "";
        char *password = "";
        char *database = "aoe";

        conn = mysql_init( NULL );

        if( !mysql_real_connect( conn, server, user, password, database, 0, NULL, 0 )) {
                fprintf( stderr, "%s\n", mysql_error( conn ));
                return 1;
        }

        if( !mysql_query( conn, "SELECT * FROM examples" )) {
                fprintf( stderr, "%s\n", mysql_error( conn ));
                return 1;
        }

        res = mysql_use_result( conn );

        while(( row = mysql_fetch_row( res )) != NULL ) {
                printf( "%s %s %s %s\n", row[ 0 ], row[ 1 ], row[ 2 ], row[ 3 ] );
        }
        mysql_close( conn );
        return 0;
}

Unfortunately, it doesn't seem to be including mysql.h, as the mysql functions and types are not recognized:

Code:

[root@crashbox examples]# gcc -Wall -o aoe aoe.c
/tmp/ccYhxlZO.o(.text+0x32): In function `main':
: undefined reference to `mysql_init'
/tmp/ccYhxlZO.o(.text+0x52): In function `main':
: undefined reference to `mysql_real_connect'
/tmp/ccYhxlZO.o(.text+0x67): In function `main':
: undefined reference to `mysql_error'
/tmp/ccYhxlZO.o(.text+0x98): In function `main':
: undefined reference to `mysql_query'
/tmp/ccYhxlZO.o(.text+0xad): In function `main':
: undefined reference to `mysql_error'
/tmp/ccYhxlZO.o(.text+0xd9): In function `main':
: undefined reference to `mysql_use_result'
/tmp/ccYhxlZO.o(.text+0xea): In function `main':
: undefined reference to `mysql_fetch_row'
/tmp/ccYhxlZO.o(.text+0x132): In function `main':
: undefined reference to `mysql_close'
collect2: ld returned 1 exit status
[root@crashbox examples]#

Any help would be greatly appreciated!

Hko 01-03-2005 02:09 PM

You forgot to link with the mysql client library.
Compile with this:
Code:

gcc -Wall -lmysqlclient -o aoe aoe.c

zaichik 01-03-2005 03:47 PM

Hi Hko,

Thanks for your reply. When I try that, here is what I get:

Code:

[root@crashbox examples]# gcc -Wall -lmysqlclient -o aoe aoe.c
/usr/bin/ld: cannot find -lmysqlclient
collect2: ld returned 1 exit status

Now what am I missing?
TIA

Hko 01-03-2005 04:29 PM

Do you have both of these installed?

mysql-3.23.54a-11.i386.rpm mysql-devel-3.23.54a-11.i386.rpm

To be on the safe side, run "ldconfig" as root after installing both.

If this doesn't help, try to find the file "libmysqlclient.so", and put the directory where you find it in the -L options of gcc.
Code:

gcc -Wall -L/path/to/the/file/ -lmysqlclient -o aoe aoe.c
The program you posted compiled fine on my Debian sarge computer.

zaichik 01-03-2005 04:53 PM

I installed MySQL-devel-4.0.23-0-i386.rpm earlier. My first attempt at compiling the program gave the error that it couldn't find mysql.h, so I figured (since I installed the client and server from RPMs) that I didn't have the header files. I figured that MySQL-devel-4-0-23-i386.rpm would provide all that I need, but here is what I get now:

Code:

[root@crashbox examples]# rpm -qa MySQL-devel
MySQL-devel-4.0.23-0
[root@crashbox examples]# ldconfig
[root@crashbox examples]# locate libmysqlclient.so
[root@crashbox examples]# locate -u
[root@crashbox examples]# locate libmysqlclient.so
[root@crashbox examples]#

As you can see, for whatever reason I don't have libmysqlclient.so, and ldconfig does nothing.

:confused:

Thanks again.

zaichik 01-03-2005 05:01 PM

Aha.

Code:

[root@crashbox examples]# rpm -i MySQL-shared-4.0.23-0.i386.rpm
warning: MySQL-shared-4.0.23-0.i386.rpm: V3 DSA signature: NOKEY, key ID 5072e1f5
[root@crashbox examples]# ldconfig
[root@crashbox examples]# locate -u
[root@crashbox examples]# locate libmysqlclient.so
/usr/lib/libmysqlclient.so.12
/usr/lib/libmysqlclient.so
/usr/lib/libmysqlclient.so.12.0.0
[root@crashbox examples]# gcc -Wall -lmysqlclient -o aoe aoe.c
[root@crashbox examples]# ./aoe
Segmentation fault
[root@crashbox examples]#

At least it compiles now. Any idea where the seg fault is coming from?

zaichik 01-03-2005 05:31 PM

OK, the problem may have been that there was not actually a table named "examples". I gave it a legitimate name and now it runs without error. However, it does nothing, as though the query "SELECT * FROM games" returned an empty set, although there are over 91 records in the table.

zaichik 01-03-2005 05:43 PM

Tweaked the code:

Code:

#include <stdio.h>
#include <mysql/mysql.h>

int main ( int argc, char **argv ) {

        MYSQL conn;
        MYSQL_RES *res;
        MYSQL_ROW row;

        char *server = "localhost";
        char *user = "";
        char *password = "";
        char *database = "aoe";

        mysql_init( &conn );

        if( !mysql_real_connect( &conn, server, user, password, database, 0, NULL, 0 )) {
                fprintf( stderr, "%s\n", mysql_error( &conn ));
                return 1;
        }

        if( mysql_query( &conn, "SELECT * FROM games" )) {
                fprintf( stderr, "%s\n", mysql_error( &conn ));
                return 1;
        }
        res = mysql_use_result( &conn );

        while(( row = mysql_fetch_row( res ))) {
                printf( "%s %s %s %s\n", row[ 0 ], row[ 1 ], row[ 2 ], row[ 3 ] );
        }


        mysql_close( &conn );

        return 0;
}

And now it works.

Thanks again for your help, Hko! You'll probably be seeing more posts from me in the near future... :rolleyes:


All times are GMT -5. The time now is 11:58 AM.