I'm testing standalone DAL usage - basically using the DAL to help
move/convert some tables from an external db.
Basically I'm trying to populate a dictionary with a subset of data from an
external database and then insert a series of data records into a new
table.
I expected to be able to populate a dictionary with a subset of the
selected fields but it appears that the db.insert method will not accept a
dictionary ( I get the error 'insert() takes exactly 1 argument (2 given').
I was able to get this to work using the bulk_insert method instead, but
this is not very intuitive. (See the code below for a specific example)
Is there a reason why the DAL insert method should/could not provide
dictionary support (I checked the docs and this is not really covered in
any detail).
>>>> Code Sample <<<<
Something like this:
# Source db = sdb()
sdb = DAL("postgres://postgres:@localhost:5432/testdb")
sdb.define_table('mytbl',
Field('id', type='integer'),
Field('fname', type='string', length=10),
Field('lname', type='string', length=10),
# Target db = ddb()
ddb = DAL("sqlite://test.db")
ddb.define_table('mytbl',
Field('id', type='integer'),
Field('fname', type='string', length=10),
Field('lname', type='string', length=10),
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]
# commit changes
try:
#ddb.mytbl.insert(ddict) # <--- error - insert() takes
exactly 1 argument (2 given)
ddb.mytbl.bulk_insert([ddict]) # <--- works
ddb.commit()
except:
ddb.rollback()