开发者

Error detection in C client program connecting MySQL

开发者 https://www.devze.com 2023-03-12 07:05 出处:网络
My C client program connects with MySQL server. I can connect, but the problem arises when I try to implement error detection codes. If I do the following (here I have intentionally put incorrect user

My C client program connects with MySQL server. I can connect, but the problem arises when I try to implement error detection codes. If I do the following (here I have intentionally put incorrect user name):

#include "mysql.h"

int main()
{
   MYSQL* conn_ptr_db;

   conn_ptr_db = mysql_init(NULL);

   if(!conn_ptr_db)
   {
       perror("Error connecting to MySQL! ");
       exit(0);
   }

   if(mysql_real_connect(conn_ptr_db,"localhost","rooti","root","mysql",0,NULL,0))
   {
       printf("Hurrah! we have connected to MySQL! ");
       mysql_close(conn_ptr_db);
   }
   else
   {
       printf("Connection failed to MySQL! \n");
       printf("Error code: %d  %s %s\n",mysql_errno(conn_ptr_db),mysql_sqlstate(conn_ptr_db),mysql_error(conn_ptr_db));
   }

   //mysql_close(conn_ptr_db);
   return 0;
}

Now the output to this program:

./db_access Connection failed to MySQL! Error code: 1045 28000 Access denied for user 'rooti'@'localhost' (using password: YES)

This is the correct output.

But if I do the following:

#include "mysql.h"

in开发者_运维知识库t main()
{
   MYSQL* conn_ptr_db;

   conn_ptr_db = mysql_init(NULL);

   if(!conn_ptr_db)
   {
       perror("Error connecting to MySQL! ");
       exit(0);
   }

   //CHANGES -------------
   conn_ptr_db = mysql_real_connect(conn_ptr_db,"localhost","rooti","root","mysql",0,NULL,0);
   if(conn_ptr_db)
   {
       printf("Hurrah! we have connected to MySQL! ");
       mysql_close(conn_ptr_db);
   }
   else
   {
       printf("Connection failed to MySQL! \n");
       printf("Error code: %d  %s %s\n",mysql_errno(conn_ptr_db),mysql_sqlstate(conn_ptr_db),mysql_error(conn_ptr_db));
   }

   //mysql_close(conn_ptr_db);
   return 0;
}

I get the following output: ./db_access Connection failed to MySQL! Error code: 0 08001

Here, the mysql_errno() and mysql_error() are not working. Why?


After the connection has failed conn_ptr_db is set to NULL. You then try and use this value as a paramter to a call to msql_errono(), in effect msql_errno(NULL). Hence the error code your getting.


Because in the second example you assign the result to conn_ptr_db.

You should not have done so.

From the documentation:

A MYSQL* connection handle if the connection was successful, NULL if the connection was unsuccessful. For a successful connection, the return value is the same as the value of the first parameter.

In the case of an unsuccessful connection (which you have), you've replaced your conn_ptr_db object with NULL then attempt to do stuff with it (mysql_error).

The documentation for mysql_error doesn't explicitly say that a NULL parameter is disallowed, but it's clear that you won't get the error relating to the original, actual connection context that you overwrote.

0

精彩评论

暂无评论...
验证码 换一张
取 消