LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 08-09-2006, 01:15 PM   #1
anroy
LQ Newbie
 
Registered: Aug 2006
Location: Tokyo
Distribution: Gentoo, Xandros
Posts: 18

Rep: Reputation: 0
MySQL C API problem


I hope this is the relevant forum for this question.

I set up MySQL 5.0.24 on an Ubuntu Linux machine, by downloading the source tarball.

For some reason I just can't seem to get mysql_init to work, it ALWAYS returns NULL!

On the MySQL site it says that this happens if there is insufficient memory. I am sure that is not the case here.

Here is my code.
Code:
#include </usr/local/mysql/include/mysql/mysql.h>
#include <stdio.h>
#include <string.h>

int main()
{
	MYSQL mysql;
	rc = mysql_init( &mysql );
	if( rc == NULL ) {
		printf( "mysql_init returns NULL" );
	}
}

I also tried the following variation, with the same NULL results.
Code:
#include </usr/local/mysql/include/mysql/mysql.h>
#include <stdio.h>
#include <string.h>

int main()
{
	MYSQL *mysql;
	mysql = mysql_init( NULL );
	if( mysql == NULL ) {
		printf( "mysql_init returns NULL" );
	}

}
Does anybody have even the slightest clue as to why I would get NULL?

I compiled and linked using the following:
gcc sql_prog.c -o sql_prog -lz -lmysqlclient -L /usr/local/mysql/lib/mysql/

The compile and link were successful, as the message "mysql_init returns NULL" is properly output!

Also, the database server runs fine, the client programs mysqlshow, mysqladmin, etc. work fine. They were also compiled on the same machine (I installed from source).

I am thinking about copying one of the client folders (like mysqlshow) in the MySQL source, modifying the code so it does what I need, and including it in the overall MySQL build as another Client.

However it is very time-consuming to configure and make the entire MySQL each time. Maybe I can use this option?
shell> ./configure --without-server

Does this have any effect on the server that I am not aware of, like removing any files or settings? I just want to rebuild the clients but want to leave the server as is.

Thanks,
Arka N. Roy
 
Old 08-09-2006, 06:09 PM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
can you get any error info with mysql_error()
 
Old 08-11-2006, 12:50 AM   #3
anroy
LQ Newbie
 
Registered: Aug 2006
Location: Tokyo
Distribution: Gentoo, Xandros
Posts: 18

Original Poster
Rep: Reputation: 0
Sorry it turns out this was a false alarm. Please forgive me for having wasted your time.

The lesson here is to check more carefully before posting to a forum.
 
Old 08-11-2006, 08:35 AM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
it could be said that another lesson would be to post the solution so that others can learn from your experience.
 
Old 08-14-2006, 02:38 AM   #5
anroy
LQ Newbie
 
Registered: Aug 2006
Location: Tokyo
Distribution: Gentoo, Xandros
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by xhi
it could be said that another lesson would be to post the solution so that others can learn from your experience.
Alas, I worried it is too silly to be of much benefit to anyone. But you are right, it can be a good example of what NOT to do.

The code snippet in my first post was not the actual story. I was unfortunately away from my source code when I posted so I just typed an approximation on the fly.

Here is the actual code I was running at the time.

Code:
int main()
{
	MYSQL*		mysql;
	MYSQL_RES*	res;
	MYSQL_ROW 	row;
	char 		query[80];

	mysql = mysql_init( NULL );

	if( mysql == NULL ) {

		mysql_real_connect( mysql, "localhost", "username", "password","dbname", 0, "/tmp/mysql.sock", 0 );

		sprintf( query, "SELECT * FROM tablename" );
		mysql_real_query( mysql, query, (unsigned int)strlen(query) );
		res = mysql_use_result( mysql );

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

		mysql_free_result( res );
		mysql_close( mysql );
	}

	else {
		printf( "mysql_init returned NULL\n" );
	}
	return 0;
}
Naturally, a successful invocation of mysql_init (ie. a non-NULL return value) would result in the output message "mysql_init returned NULL".

The condition
if( mysql == NULL )
was simply a typo.

I fixed it to be
if( mysql != NULL )

So another lesson here is to literally copy-paste source code AS IS when posting on forums, rather relying on recollection or providing approximations. Having said that, even now I had to doctor a couple of areas in the code, such as username and password etc.
 
Old 08-14-2006, 09:36 AM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
congrats on getting it to work!

and good work on following up with the solution. you can be sure that someone will benefit from seeing a full example like this at some point in the future.

-x
 
Old 12-07-2006, 08:23 PM   #7
CrazyHorse
LQ Newbie
 
Registered: Jul 2003
Location: NZ
Distribution: Ubuntu
Posts: 22

Rep: Reputation: 15
and good work on following up with the solution. you can be sure that someone will benefit from seeing a full example like this at some point in the future.

And you were right!! Thanks!

 
  


Reply

Tags
mysql



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
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
python DB-API for MySQL install problem SheldonPlankton Programming 1 07-27-2004 05:55 PM
msql++ c++ api for mysql nakkaya Linux - Software 0 04-19-2003 03:56 PM
c++ and mysql api nakkaya Programming 4 03-28-2003 11:10 PM

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

All times are GMT -5. The time now is 05:51 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