BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Saving data from a Vector Collection

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Anthony
Guest





PostPosted: Sun Dec 10, 2006 3:09 pm    Post subject: Saving data from a Vector Collection Reply with quote



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...
Back to top
Clayton Arends
Guest





PostPosted: Mon Dec 11, 2006 12:22 am    Post subject: Re: Saving data from a Vector Collection Reply with 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
Back to top
Anthony
Guest





PostPosted: Mon Dec 11, 2006 6:51 am    Post subject: Re: Saving data from a Vector Collection Reply with 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. 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" <nospam_claytonarends (AT) hotmail (DOT) com> wrote in message
news:457c5019$1 (AT) newsgroups (DOT) borland.com...
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
Back to top
Clayton Arends
Guest





PostPosted: Mon Dec 11, 2006 8:01 am    Post subject: Re: Saving data from a Vector Collection Reply with quote

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
Back to top
Anthony
Guest





PostPosted: Mon Dec 11, 2006 9:10 am    Post subject: Re: Saving data from a Vector Collection Reply with quote

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...
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.