Re: [Cppcms-users] Is dbixx::row available in an associative arary?
Brought to you by:
artyom-beilis
From: Artyom <art...@ya...> - 2010-12-13 09:30:58
|
> > Hello, > > > http://art-blog.no-ip.info/wikipp/en/page/ref_dbixx_row > It's not too clear from the documentation above if the data in dbixx::row is > available from an associative array. > > dbixx::result res; > sql<<"SELECT name,age,birthday " > "FROM person ", res; > > dbixx::row myRow; > while (res.next(myRow)) { > // Can we do something like the following? > std::string name = myRow["name"]; > // ... > } No you can't. The reason is simple - what type should operator[] should return? Today operator[] overloaded to check if the variable is null. > > My problem is that I want to pass the row data into my classes' constructors. > > Currently, I must declare temporary variables on the stack, extract the row > data into those variables, and pass them to my object constructors: > > > dbixx::row myRow; > while (res.next(myRow)) { > std::string tmp_name; > int tmp_age, tmp_birthday; > myRow >> tmp_name >> tmp_age >> tmp_birthday; > // Call my class constructor: > myClass person = myClass(tmp_name, tmp_age, tmp_birthday); > } > > I'd be neater if we could do something like this: > > > > dbixx::row myRow; > while (res.next(myRow)) { > // Call my class constructor: > myClass person = myClass(myRow["name"], myRow["age"], myRow["birthday"]); > } > > I know there is the method fetch(): > bool fetch(int idx,int &value); > but again, it requires temporary variables to be created, adding to code bloat > > and overheads... :-/ > > > Or how do you handle such things? > > Thanks, > > Augustin. > I've added an option to fetch a value by column index (not name) as r.get<std::string>(1), take a look on the version in trunk. However... I'd suggest to take a look on cppdb - this is a new project drop in replacement of dbixx, unlike dbixx it does not depend on libdbi library but rather implements all by its own. Its API is different from dbixx but conversion is mostly mechanical. There you can do: cppdb::result r = sql << "SELECT name,age,birthday FROM users"; while(r.next()) { myClass person = myClass(r.get<std::string>("name"), r.get<int>("age"), r.get<std::tm>("birthday")); } You may check the beta version at: https://cppcms.svn.sourceforge.net/svnroot/cppcms/cppdb/trunk The documentation is avalible via Doxygen. So after checkout run doxygen and take a look on the file html/index.html for docs. i.e.: svn co https://cppcms.svn.sourceforge.net/svnroot/cppcms/cppdb/trunk cppdb cd cppdb doxygen cd html firefox index.html It will be officially released withing a week (only some last toches on documentation should be done) Regards, Artyom |