www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How it's better to store data from DB?

reply Suliman <evermind live.ru> writes:
Usually I am storing daba from DB as array of structures. 
Something like:

struct MyData
{
  int id;
  string name;
  int age;
}

MyData mydata;

Then I am creating array of structures:
MyData [] mydatas;

And fill data in my code. Then append it to `mydatas` to get it 
iterable.

But is it's good? Maybe there there is better way?

I need main -- be able iterate thru the data. Any other variants?
Sep 05 2016
parent Lodovico Giaretta <lodovico giaretart.net> writes:
On Monday, 5 September 2016 at 10:00:26 UTC, Suliman wrote:
 Usually I am storing daba from DB as array of structures. 
 Something like:

 struct MyData
 {
  int id;
  string name;
  int age;
 }

 MyData mydata;

 Then I am creating array of structures:
 MyData [] mydatas;

 And fill data in my code. Then append it to `mydatas` to get it 
 iterable.

 But is it's good? Maybe there there is better way?

 I need main -- be able iterate thru the data. Any other 
 variants?
It depends on your specific use case. My advice is to try *not* to store the data in memory. I mean, if your first operation on the array is to filter it (but that should be done directly in the sql queries) or if you manage to do everything while iterating it only once, then you can avoid creating the array. Create a range that wraps the query result set and iterate it. This way you maintain in memory just one element at a time. This is akin to what you usually do in JDBC (I used that a lot, while in D I don't have any db experience). But of course there are cases in which this advice is not applicable.
Sep 05 2016