Hello,

I need to write a simple C client for a project. I am using the MySQL C
API. Attached is the code. It occassionally segfaults with no visible
pattern. Could someone help me figure out why? Or any other comments on the
code to help me make it better?

/* client.c */

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

int main()
{
        /* declare
structures and variables */
        char query[255];
        int i, j, count;

MYSQL mysql;
        MYSQL_RES *result;
        MYSQL_ROW row;
        MYSQL_FIELD
*field;

        /* initialize MYSQL structure */

mysql_init(&mysql);

        /* connect to database */
        if
(!(mysql_real_connect(&mysql, NULL, "root", "", "db1", 0, NULL, 0)))

 {
                fprintf(stderr, "Error in connection: %s\n",
mysql_error(&mysql));
        }

        for( ;; )
        {

printf("query? ");
                gets(query);
                if (strcmp(query,"exit")
== 0) 
                {
                        break;
                }

                /* execute query
*/
                /* if error, display error message */
                /* else check the type of
query and handle appropriately */
                if (mysql_query(&mysql, query) != 0)

{
                        fprintf(stderr, "Error in query: %s\n", mysql_error(&mysql));
                }

else
                {
                        if (result = mysql_store_result(&mysql))
                        {
                                /* SELECT
query */
                                /* retrieve result set */
                                int numRecords =
mysql_num_rows(result);
                                int numFields = mysql_num_fields(result);

for (i = 0; i < numRecords; i++)
                                {
                                        row =
mysql_fetch_row(result);

                                        for (j = 0; j < numFields; j++)
                                        {

//field= mysql_fetch_field(result);
                                                fprintf(stdout, "%s", row[j]);

                        j != (numFields-1) ? printf(", ") : printf("\n");
                                        }
                                }

fprintf(stdout, "** Query successful, %d rows retrieved **\n",
numRecords);
                        }
                        else
                        {
                                if (mysql_field_count(&mysql) == 0)

{
                                        /* non-SELECT query */
                                        fprintf(stdout, "** Query successful, %d
rows affected **\n", mysql_affected_rows(&mysql));
                                }
                                else
                                {

        fprintf(stderr, "Error in reading result set: %s\n",
mysql_error(&mysql));
                                }
                        }
                }

                /* clean up */

mysql_free_result(result);
        }
        mysql_close(&mysql);
}

--
I wouldn't recommend sex, drugs, and insanity for everyone, but it works
for me.


-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to