A Simple Query Program

Now that you understand how to retrieve a resultset and check whether it was fetched without error, let's update the programs so that you can compile and run them with their new functionality.

The common.c and common.h programs are the same as before, although main.c now has more code, as shown in Listing 13.4.

Listing 13.4. main.c
 1: /* main.c */
 2: /* Connect, run a query and check the result set */
 3:
 4: #include <stdio.h>
 5: #include <mysql.h>
 6: #include "common.h"
 7:
 8: #define def_db_host     NULL
 9: #define def_db_user     NULL
10: #define def_db_name     NULL
11: #define def_db_port     0
12: #define def_unix_socket NULL
13:
14: MYSQL   *mysql;
15:
16: int main (int argc, char *argv[])
17: {
18: MYSQL_RES   *res_set;
19: char        *db_pass;
20:
21:     db_pass = get_tty_password (NULL);
22:
23:     mysql = db_connect (
24:         def_db_host,
25:         def_db_user,
26:         db_pass,
27:         def_db_name,
28:         def_db_port,
29:         def_unix_socket,
30:         0);
31:     if (mysql == NULL) {
32:         exit (1);
33:     } else {
34:         fprintf (stdout, "Connected.
");
35:
36:         if (mysql_query (mysql, "SHOW DATABASES") == 0) {
37:             printf ("Query succeeded. About to fetch result.
");
38:             res_set = mysql_store_result (mysql);
39:
40:             if (res_set != NULL) {
41:                 /* Result set obtained, so process */
42:
43:                 /* Do something with the result set */
44:
45:                 mysql_free_result (res_set);
46:             } else {
47:                 /* No result set, see what happened */
48:                 if (mysql_field_count (mysql) > 0) {
49:                     /* Should have had some fields
50:                     * so an error must have occurred */
51:                     fprintf (stderr,
52:                         "No result set, mysql_store_result() failed: %s
",
53:                         mysql_error (mysql));
54:                 } else {
55:                     /* Field count is 0,
56:                     so no result was expected from the query */
57:                     printf ("Query was not meant to return data.
");
58:                 }
59:             }
60:         } else {
61:             fprintf (stderr,
62:                 "Query failed: %s
", mysql_error(mysql));
63:         }
64:
65:     }
66:
67:     db_disconnect (mysql);
68:     exit (0);
69: }
					

Lines 36 through 63 should look familiar; they are almost identical to the code example you looked at previously. Notice the function mysql_free_result() in line 45. When you finish processing your resultset, you must use this to free up the memory allocated as a result of functions such as mysql_store_result() or mysql_use_result().

Note

There are also other functions that produce resultsets that we've not covered here.

For example, the API has functions to retrieve information about fields (columns) in a resultset and information about the client, server, and connection protocol. There are also functions to list databases on the server, to show information about processes running on the server, and so on.

Consult the MySQL Web site or the MySQL Technical Reference for more information about other available functions.


mysql_free_result() has the syntax

mysql_free_result (*result)

where *result is a resultset of the datatype MYSQL_RES. It doesn't return a value, and there's no need to check for errors.

You should be able to compile and run this program, although its output still shows only the connection routine and does not display any data from our query. Let's see how to do that.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.182.150