Esql Query
Posted in 2006
Topics: SQL Development & Query Writing, Connectivity: ESQL/C, 4GL & Embedded SQL
Hi I have a C++ program which uses esql to access the database. In essence, I would like to query a "Customer" table for specific fields (name and identity) and would like to return a list of entries (CustomerDetails) back to my C++ program. To this end, I see the esql method returning an array of CustomerDetails. Below is a view of how I see this to work. class CustomerDetails { string name; int id; } void main () { CustomerDetails *ptr; int size; sqlGetCustomerDetail ( ptr, size); // iterate through the array // extract the details and perform free on ptr. for ( .... ) { free ptr; } } /* sqlGetCustomerDetail () is in ec file */ /* ptr will point to the first instance of CustomerDetail and size will indicate how many entries there are */ int sqlGetCustomerDetail ( CustomerDetails *ptr, int &size ) { /* make query on customer table */ /* get number of rows returned */ /* create a CustomerDetails array of size based on the number of rows returned */ ptr = malloc ( CustomerDetails * number of rows); /* populate each ptr with from details of each row returned */ for ( ... ) { ptr->name = Name; ptr->id = Idenity; ptr++; } } My questions :- 1) In sqlGetCustomerDetail (), before I iterate through each row, is there a way of determining how many rows the query has returned so that I can allocate correct memory via malloc ? 2) please correct if am wrong ( or completely wrong ) and is there a better approach to this? Thank you all in advance for your help Pete.
On 17 Mar 2006 22:06:20 -0800, vwbora@onetel.com wrote: > I have a C++ program which uses esql to access the database. In > essence, I would like to query a "Customer" table for specific fields > (name and identity) and would like > to return a list of entries (CustomerDetails) back to my C++ program. > To this end, I see the esql method returning an array of > CustomerDetails. > > Below is a view of how I see this to work. > > class CustomerDetails { > string name; > int id; > } > > void main () > { > > CustomerDetails *ptr; > int size; > > sqlGetCustomerDetail ( ptr, size); > > // iterate through the array > // extract the details and perform free on ptr. > for ( .... ) > { > free ptr; > } > } > > /* sqlGetCustomerDetail () is in ec file */ > /* ptr will point to the first instance of CustomerDetail and > size will indicate how many entries there are */ > > int sqlGetCustomerDetail ( CustomerDetails *ptr, int &size ) > { > /* make query on customer table */ > > /* get number of rows returned */ > > /* create a CustomerDetails array of size based on the number of > rows returned */ > > ptr = malloc ( CustomerDetails * number of rows); > > /* populate each ptr with from details of each row returned */ > for ( ... ) > { > ptr->name = Name; > ptr->id = Idenity; > > ptr++; > } > } > > My questions :- > > 1) In sqlGetCustomerDetail (), before I iterate through each row, is > there > a way of determining how many rows the query has returned so that > I can allocate correct memory via malloc ? No, but why wouldn't you be using a Vector<CustomerDetails> to handle the allocations automatically? Yes - you do a SELECT COUNT(*) with the same criteria that you intend to use in the detail fetching query. If you run with repeatable read isolation (in a database with logging), then the count and the query will process the same sets of data - and hence produce consistent answers. At lower levels of isolation, other people could add or remove records between the count and the main query, so the count could be wrong. > 2) please correct if am wrong ( or completely wrong ) and is there a > better approach to this? In C, main() returns an int; I thought the same was true of C++. -- Jonathan Leffler #include <disclaimer.h> Email: jleffler@earthlink.net, jleffler@us.ibm.com Guardian of DBD::Informix v2005.02 -- http://dbi.perl.org/
Your suggestion of using a vector is very good. As I am not entirely comfortable with esql, I am not sure what can and can't be done and hence whether I could use a Vector in this way. So just to clarify ..... Using a vector, I could pass a reference of Vector<CustomerDetails> to the esql method. Within the esql method, I could add entries to the Vector ( via add). The add operation will reserve memory for entry. Back in my C++ program, I can iterate through the Vector to read each entry, When the vector goes out scope, it should release any memory it had reserved. No need to dynamically allocate or free of memory. ? Does the vector need to be have a size defined intially ? Much neater approach ... Thank you.
Related threads
- the longer you surf, the MORE $$$ you earn !!
- Store procedure
- emulation for Vt100
- extent size questions again ...