LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   MySQL C API problem (http://www.linuxquestions.org/questions/programming-9/mysql-c-api-problem-472361/)

anroy 08-09-2006 01:15 PM

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

xhi 08-09-2006 06:09 PM

can you get any error info with mysql_error()

anroy 08-11-2006 12:50 AM

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. :o

xhi 08-11-2006 08:35 AM

it could be said that another lesson would be to post the solution so that others can learn from your experience.

anroy 08-14-2006 02:38 AM

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.

xhi 08-14-2006 09:36 AM

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

CrazyHorse 12-07-2006 08:23 PM

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!

:D


All times are GMT -5. The time now is 04:29 AM.