List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:July 18 1999 4:54pm
Subject:Re: Basic mysql C client question
View as plain text  
At 10:44 PM -0500 7/17/99, Chris Frost wrote:
>On Sat, Jul 17, 1999 at 10:20:21PM -0500, Paul DuBois wrote:
>> One big problem with your program is that you do almost no error
>> checking.  What happens if mysql_real_connect() fails.  You're
>> going to crash, because the program continues on as if nothing
>> is wrong.

>As I said I've been working on it for a few days (it did start simple),
>and for this detecting failures isn't important except for getting to run,
>but thanks for the advice.

Well ... no.

Error checking is always in order.  Otherwise you end up running around
trying to figure out why your program doesn't work, or works erratically,
and whether the failure is really at the apparent point of failure,
or due to something that didn't work earlier in the program, etc.

Which is exactly where you are right now, correct? :-)


>
>> >query_out_ptr = mysql_store_result(&mysql); /* store output from
>> >						   * query */
>>
>> The program you attached actually has those lines as:
>>
>> 	/* FIXME: Good chance this is where our main problem lays */
>> 	query_out_ptr->data = mysql_store_result(&mysql); /* store output of
>> 						       * query */
>>
>> which is incorrect.  The ->data should be removed, so the lines
>> you included in your message are correct.
>A friend has been trying to help with this a bit and they suggested trying
>to pull data instead of the entire structure (the actual code in the
>attachment won't compile with the above lines of course). Thanks for letting
>me know the lines in my message were correct though (yes!).

Perhaps you could ask your friend for some working code that uses ->data
in the manner suggested.

The MYSQL_RES is best treated as a black box, accessed only through
the API functions intended to work with it.

>> >or
>> >printf("%s \n",query_out.data); /* print our query data */
>>
>> You shouldn't even be trying to access query_out.data.  Why are you
>> doing this?  If you're trying to get the rows returned by your query
>> and print their contents, use mysql_fetch_row().  That seems to be
>> what you're doing later in your program.
>In the first part of the program I'd just like to print the query output
>in one simply glob (just to show the user what was received). How would
>one do this? (Or is the method I later used the only way?)

You don't print query output as a glob.  You access it row by row, and
within a row you access individual columns.

>> Also, you have this early in the program:
>>
>> 	/* length of a returned field. we have no idea how long it is, but 100
>> 	 * sounds like it may work (hmm, this isn't ugly) */
>> 	/* sample code simply stored this value into an unreferenced pointer */
>> 	unsigned long lengths[100], *lengths_ptr;
>>
>>
>> Which you use later in your row-fetching loop as:
>>
>> 	while ((row = mysql_fetch_row(query_out_ptr)))
>> 	{
>> 		printf("\tDebug: while loop %d in progress.\n", row);
>> 		printf("\tDebug: %d == row ==
>> mysql_fetch_row(query_out_ptr)\n",
>> 				row);
>> 		lengths_ptr = mysql_fetch_lengths(query_out_ptr);
>> 		printf("\tDebug: for loop beginning.\n");
>> 		/* number of columsn is one if only one total, but
>> 		 * since we start at 0 (one less), we only do a less
>> 		 * than and not an equals */
>> 		/* debuging note: if you tell this to loop however many
>> 		 * times as there are columns, it will return all
>> 		 * data */
>> 		printf("\t\t");
>> 		for (i = 0; i < (int) num_query_columns; i++)
>> 		{
>> 			printf("[%.*s] ", (int) lengths[i], row[i] ?
>> 					row[i] : "NULL");
>> 		}
>> 		printf("\n"); /* return character for above printf,
>> 			       * this way we can print out multiple
>> 			       * columns*/
>> 		printf("\tDebug: for loop completed. (%d loop(s).)\n",
>> 				i);
>> 	}
>>
>> So you fetch the column lengths into lengths_ptr, but then in the
>> print loop you use lengths.  lengths is never set to anything.
>> You should be using lengths_ptr[i] in the printf() statement.

>After defining variables I setup pointers, and store the address of
>lenghts in lengths_ptr, so isn't lengths[i] == lengths_ptr + i?

No.  Boiled down, what you have in your program is:

	unsigned long lengths[100], *lengths_ptr;

	...

	lengths_ptr = lengths;

	...

	while ((row = mysql_fetch_row(query_out_ptr)))
	{
		lengths_ptr = mysql_fetch_lengths(query_out_ptr);
		for (i = 0; i < (int) num_query_columns; i++)
		{
			printf("[%.*s] ", (int) lengths[i], row[i] ?
					row[i] : "NULL");
		}
	}

You set lengths_ptr to lengths, then assign it to the return value
of mysql_fetch_lengths().  That second assignment simply changes the
value of lengths_ptr.  It doesn't modify the contents of the lengths[]
array.

Dump lengths[], you don't need it in your program.  In the printf()
statement, use lengths_ptr[i], not lengths[i].

--
Paul DuBois, paul@stripped
Thread
Basic mysql C client questionChris Frost18 Jul
  • Re: Basic mysql C client questionPaul DuBois18 Jul
    • Re: Basic mysql C client questionChris Frost18 Jul
      • Re: Basic mysql C client questionPaul DuBois18 Jul
  • Re: Basic mysql C client questionEd Carp18 Jul
    • Re: Basic mysql C client questionSasha Pachev18 Jul