Board index » cppbuilder » Saving data from a Vector Collection

Saving data from a Vector Collection


2006-12-10 05:09:31 PM
cppbuilder5
I have a collection that is populated from a dataset but I am not sure how
to update the data back to the database. Can anyone point me in the right
direction at all???
Thanx...
 
 

Re:Saving data from a Vector Collection

Are the dataset contents still in memory or is the dataset possibly closed
between the time you populated your collection and the time that you would
like to update the database?
If the dataset is still in memory, and the dataset is editable, then perhaps
you can store the row number of each record with the object in the
container. In this way you can perform the following:
struct TMyData
{
int RecNo;
... // the rest of the data
};
typedef std::list<TMyData>TMyData_list;
TMyData_list mylist;
// populate the container from your dataset
for (; !DataSet1->Eof; DataSet1->Next())
{
mylist.push_back();
TMyData& obj = mylist.back();
obj.RecNo = DataSet1->RecNo;
... // the rest of your assigns/reads
}
... // code that does something with the container
// Update the contents of the dataset
for (TMyData_list::iterator it = mylist.begin(), end = mylist.end();
it != end; ++it)
{
TMyData& obj = *it;
// if possible, check if obj has been modified first
// seek the correct record and update
DataSet1->RecNo = obj.RecNo;
DataSet1->Edit();
... // set the values appropriately
DataSet1->Post();
}
If the dataset is non-editable then you may have to construct a series of
update queries on some known index. If you post a little more background
for your problem then maybe the correct solution can be presented.
HTH,
- Clayton
 

Re:Saving data from a Vector Collection

Hi thanx for the help there, the data set is not in memory, but i can call
it again and then use it and it is editable. Just as a matter of interest
though how could i use queries to send the data back, the objects do contain
a known index which would be okay to use. thanx again...
"Clayton Arends" < XXXX@XXXXX.COM >wrote in message
Quote
Are the dataset contents still in memory or is the dataset possibly closed
between the time you populated your collection and the time that you would
like to update the database?

If the dataset is still in memory, and the dataset is editable, then
perhaps you can store the row number of each record with the object in the
container. In this way you can perform the following:

struct TMyData
{
int RecNo;
... // the rest of the data
};

typedef std::list<TMyData>TMyData_list;

TMyData_list mylist;

// populate the container from your dataset
for (; !DataSet1->Eof; DataSet1->Next())
{
mylist.push_back();
TMyData& obj = mylist.back();
obj.RecNo = DataSet1->RecNo;
... // the rest of your assigns/reads
}

... // code that does something with the container

// Update the contents of the dataset
for (TMyData_list::iterator it = mylist.begin(), end = mylist.end();
it != end; ++it)
{
TMyData& obj = *it;

// if possible, check if obj has been modified first

// seek the correct record and update
DataSet1->RecNo = obj.RecNo;
DataSet1->Edit();
... // set the values appropriately
DataSet1->Post();
}

If the dataset is non-editable then you may have to construct a series of
update queries on some known index. If you post a little more background
for your problem then maybe the correct solution can be presented.

HTH,
- Clayton

 

{smallsort}

Re:Saving data from a Vector Collection

Quote
Hi thanx for the help there, the data set is not in memory, but i can call
it again and then use it and it is editable.
There are no guarantees that the record numbers will be the same if you
reopen the dataset. The solution I showed will only work for a dataset that
remains open while the code processes the data in the container.
Quote
Just as a matter of interest though how could i use queries to send the
data back,
the objects do contain a known index which would be okay to use. thanx
again...
I don't know what type of database you are working against so I will just
show the generic TQuery approach. Some details may differ depending on your
implementation.
#include <memory>
struct TMyData
{
sometype Value1;
sometype Value2;
sometype IndexValue;
bool Modified() const; // Implement somehow if you want
};
typedef std::list<TMyData>TMyData_list;
//
TQuery* query = new TQuery(NULL);
std::auto_ptr<TQuery>auto_query(query);
query->SQL->Add("update tablename");
query->SQL->Add( "set field1 = :val1,");
query->SQL->Add( "field2 = :val2");
query->SQL->Add( "where indexfield = :indexval");
query->Prepared = true;
for (TMyData_list::iterator it = mylist.begin(), end = mylist.end();
it != end; ++it)
{
TMyData& obj = *it;
//
if (!obj.Modified())
continue;
//
query->Params[0] = obj.Value1;
query->Params[1] = obj.Value2;
query->Params[2] = obj.IndexValue;
query->ExecSQL();
}
Alternatively, you can keep track of which fields changed and create an
update query on the fly that sets just those fields.
For future reference when you reply please trim the quotes from the post you
are replying to. There was no need to reference my entire post.
HTH,
- Clayton
 

Re:Saving data from a Vector Collection

Oh sorry about that. I think that will definately get me going, think i will
go with the queries for this one, but at least i have both scenarios covered
as the other is likely to come up, thanx again for your help...