Here is the corrected version:)

Take a look at Paul DuBois book. Don't use gets. Instead use fgets. That's safe. I didn't change your gets call:)

hth,

Ganbold


#include <stdio.h> #include <stdlib.h> #include <string.h>

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif

#ifdef WIN32

#pragma  comment(lib,"ws2_32")
#include "d:\mysql\include\mysql.h"

#else

#include "/usr/local/include/mysql/mysql.h"

#endif


int main(void) { char query[255]; int i, j, count;

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


/* initialize MYSQL structure */


mysql = mysql_init(NULL);

if(mysql==NULL){
fprintf(stderr, "mysql_init failed! Error %u (%s)\n",mysql_errno(mysql),mysql_error(mysql));
exit(1);
}
if(mysql_real_connect(mysql,"localhost","root","test","test",3306,NULL,0)==NULL){


fprintf(stderr, "mysql_real_connect failed! Error %u (%s)\n",mysql_errno(mysql),mysql_error(mysql));
exit(1);
}


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

        /* execute query */
        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 = (int)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++)
                    {
                            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);


        return 0;
}




At 09:18 AM 9/19/2003 +0500, you wrote:
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]


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



Reply via email to