On Fri, Sep 19, 2003 at 09:18:22AM +0500, Vikram Vaswani 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?
You know, I used to read this mailing list religiously before it became
flooded with W32 questions and PHP users. And I've stay susbscribed
but I haven't posted to it in many many months. iIn fact, I didn't notice
I wasn't any longer subscribed. So I'm writing this application in C
and GTK and I was thinking, it's finally time to learn to write some MYSQL
C API stuff.
I've written a lot of Oracle C programs in years past. And now I'm looking
at the C API stuff and wow, it is not readly understandable. I open up
the mysql mail file with mutt, and bang, this is on the top!
I was going to ask the list if anyone has an exmaple of the basic needs
for a MYSQL program which makes a connection, sends a querry. Checks the
potention errors, and maps the most basic column types to C types.
The docs say to look at examples in the source directory, but those
aren't yet clear to me to understand.
Ruben
>
> /* 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]
--
__________________________
Brooklyn Linux Solutions
So many immigrant groups have swept through our town
that Brooklyn, like Atlantis, reaches mythological
proportions in the mind of the world - RI Safir 1998
DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
http://fairuse.nylxs.com
http://www.mrbrklyn.com - Consulting
http://www.inns.net <-- Happy Clients
http://www.nylxs.com - Leadership Development in Free Software
http://www2.mrbrklyn.com/resources - Unpublished Archive or stories and articles from
around the net
http://www2.mrbrklyn.com/downtown.html - See the New Downtown Brooklyn....
1-718-382-0585
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED]