Retrieving the data
We are now in a position to write our first program that retrieves data from the database. We're going to select the contents of all rows for which age is greater than 5. Unfortunately we don't know how to process this data yet, so all we can do it loop round retrieving it. This is select1.c :
|
The important section, where we retrieve a result set and loop through the retrieved data, is highlighted.
Retrieving the data one row at a time
To retrieve the data row by row, as we require it, rather than fetching it all at once and storing it in the client, we can replace the mysql_store_result call with mysql_use_result :
|
This function also takes a connection object and returns a result set pointer, or NULL on error. Like mysql_store_result , this returns a pointer to a result set object; the crucial difference though, is that it hasn't actually retrieved any data into the result set when it returns, just initialized the result set ready to receive data.
Resources
- This article is extracted from Chapter 5 of Professional Linux from Wrox Press Ltd
View Executing SQL statements in MySQL databases using C Discussion
Page: 1 2 3 4 5 Next Page: Excerpt from Professional Linux Programming