LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-03-2005, 01:42 PM   #1
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
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!
 
Old 01-03-2005, 02:09 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
You forgot to link with the mysql client library.
Compile with this:
Code:
gcc -Wall -lmysqlclient -o aoe aoe.c

Last edited by Hko; 01-03-2005 at 02:11 PM.
 
Old 01-03-2005, 03:47 PM   #3
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
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
 
Old 01-03-2005, 04:29 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.

Last edited by Hko; 01-03-2005 at 05:46 PM.
 
Old 01-03-2005, 04:53 PM   #5
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
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.



Thanks again.
 
Old 01-03-2005, 05:01 PM   #6
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
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?

Last edited by zaichik; 01-04-2005 at 07:00 PM.
 
Old 01-03-2005, 05:31 PM   #7
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
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.

Last edited by zaichik; 01-04-2005 at 07:01 PM.
 
Old 01-03-2005, 05:43 PM   #8
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
best desktop mysql client hlslaughter Linux - Software 10 10-18-2005 06:28 PM
MySQL Client API I_AM Linux - Newbie 1 06-27-2005 02:52 AM
mysql client api I_AM Linux - Newbie 1 06-23-2005 11:48 PM
MySQL client in C--small problem zaichik Programming 4 02-04-2005 07:23 AM
trying to insatall mysql client without the server juanb Programming 0 09-13-2004 05:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration