Project

General

Profile

How to handle Wt::Dbo in a loop?

Added by ivan jobs over 14 years ago

Hi there,

My code doesn't work.

1:

for(dbo::collection::const_iterator i = wordIDCollection.begin();i != wordIDCollection.end(); ++i)

{

dbo::backend::Sqlite3 sqlite3_("WordReciter.db");

dbo::Session session_;

session*.setConnection(sqlite3*);

session_.mapClass("Word");

dbo::Transaction transac(session_);

dbo::ptr word = session_.query< dbo::ptr >("select * from Word").where("WordID = ?").bind(*i);

transac.commit();

Word *wd = new Word();

wd~~wordID = word>wordID;

wd
wordBody = word>wordBody;

wd
wordMeaning = word~~>wordMeaning;

words.push_back(wd);

}

2:

dbo::backend::Sqlite3 sqlite3_("WordReciter.db");

dbo::Session session_;

session*.setConnection(sqlite3*);

session_.mapClass("Word");

for(dbo::collection::const_iterator i = wordIDCollection.begin();i != wordIDCollection.end(); ++i)

{

dbo::Transaction transac(session_);

dbo::ptr word = session_.query< dbo::ptr >("select * from Word").where("WordID = ?").bind(*i);

transac.commit();

Word *wd = new Word();

wd~~wordID = word>wordID;

wd
wordBody = word>wordBody;

wd
wordMeaning = word~~>wordMeaning;

words.push_back(wd);

}

What's wrong with my code? Please help me. Thanks.


Replies (2)

RE: How to handle Wt::Dbo in a loop? - Added by David Hubbard about 14 years ago

Ivan

You may have fixed this by now, and not sure in what way your "code doesn't work".

I'm just getting into Wt::Dbo myself - and thought I'd input on two things:

1) noticed you were doing a query (inside a transaction) inside the for loop on a dbo::collection.

Have you tried

a) moving the transaction boundaries outside of the for loop? - as dbo using optimistic locking and you are simply reading that may be ok or

b) if you have an outer transaction, tried copying the collection into a std::vector first - as outlined in [[[http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Dbo_1_1collection.html#_details]]] - that way you could free the outer transaction.

2) wondered if your "bind(*i)" was correct - might need to be ".bind((*i).id())" as in

dbo::ptr<Word> word = session_.query< dbo::ptr<Word> >("select * from Word").where("WordID = ?").bind((*i).id());

There is a possibility that this might be "(*i)id()\" since (I think) the "." deference gives access to the functions of the Dbo::ptr and the \">" gives you access to the attributes of the Object in the database - they may have the same value.

Having said that I'm a little bit unclear where your "WordID" (or is it "wordID" - note different cases) comes from - Wt::Dbo defines an "id" by default on tables (I'm using that in my project) - so of course it could be:

dbo::ptr<Word> word = session_.query< dbo::ptr<Word> >("select * from Word").where("wordID = ?").bind((*i)->wordID);

Cheers

Dave

RE: How to handle Wt::Dbo in a loop? - Added by Koen Deforche about 14 years ago

Hey Ivan,

Are you still stuck on this ?

Regards,

koen

    (1-2/2)