>
> def  populate_table():
>   for row in sdb().select(sdb.mytbl.ALL):
>     # build a dict for tbl insert
>     for fldname in sdb().mytbl.fields:
>         ddict[fldname] = row[fldname]
>

Note, each row of the select result is a web2py Row object (see 
http://web2py.com/books/default/chapter/29/6#Query,-Set,-Rows), which 
inherits from dict -- so you generally don't need to convert it to a dict 
because it already acts like one.

If you do need to convert to a dict (which I don't think is necessary in 
this case), you can convert using the as_dict() method, and you can convert 
an entire Rows object to a list of dicts via the as_list() method. 
See http://web2py.com/books/default/chapter/29/6#as_dict-and-as_list.

In terms of simplifying the code, it would probably be easiest to just use 
bulk_insert (which technically inserts one record at a time via a list 
comprehension):

ddb.mytbl.bulk_insert(sdb().select(sdb.mytbl.ALL).as_list())

Note, I'm not sure the .as_list() is necessary with bulk_insert -- it might 
accept the Rows object without converting to a list of dicts.

Finally, to copy the contents of a table from one db to another, another 
option is the CSV export and import 
functionality: 
http://web2py.com/books/default/chapter/29/6#CSV-(one-Table-at-a-time). 

Anthony

Reply via email to