views:

74

answers:

3

hi,

Can you give me a idea about the best way that can i store the data coming from the database dynamically. I know the number of columns before, so i want to create a data structure dynamically that will hold all the data, and i need to reorganize the data to show output. In one word - when we type the query "select * from table" - results will come. how to store the results dynamically. (Using structures , map , lists ..) . Thanks in advance.

A: 

In a nutshell, the data structures you use to store the data really depend on your data usage patterns. That is:

  • Is your need for the data simply to output it? If so, why store the data at all?
  • If not, will you be performing searches on the data?
  • Is ordering important?
  • Will you be performing computations with the data?
  • How much data will you need to hold?
  • etc...
Michael Goldshteyn
yup.. thinking in that way also, present now focus is how to store data dynamically, if possible with column names also...
jony
A: 

An array of strings (StringList in Delphi, not sure what you have in C++), one per row, where each row is a comma-separated string. This can be easily dumped and read into Excel as a .csv file, imported into lots of databases. Or, an XML document may be best. Or something else. "it depends...."

Chris Thornton
A: 

There are quite a number of options for you preferrably from the STL. Use a class to store one row in a class object or in a string if you don't want to create objects if rows are quite huge and you don't need to access all rows returned.

1) Use a vector - Use smart pointers(shared_ptr) to create objects of the class and push them in a vector. Because of the copying involved in a vector I would use a shared_ptr. Sort it later

2) Use a map/set - Creating and inserting elements may be costly, if you are looking for faster inserts. Look up maybe faster.

3) Hash map - Insertion and look up time is better than a map/set.

DumbCoder
can you elaborate 3rd point..
jony
@jony - http://publib.boulder.ibm.com/infocenter/comphelp/v9v111/index.jsp?topic=/com.ibm.xlcpp9.aix.doc/standlib/stl_unordered_map.htm.
DumbCoder
@jony - See the Performance analysis section. http://en.wikipedia.org/wiki/Hash_table . Look up time is log n (base 2) for a map/set, in the compiler implementation. Insertion maybe slow because you insert an element in its correct place, which may require to change the structure of the underlying red black tree.
DumbCoder