Reputation: 2523
I have a Dict containing some data I want to update in db, my dict is like:
user[column_name] = value
>>> user
{'id': 2, 'name': 'Carlo', city='333'}
and have a variable number of keys (depending on how many fields the user fills), and the table contains more columns than the values I want to update.
What I'm trying to do is this:
>>> db.update('users', where="id=$id", **user)
0.0 (19): UPDATE users SET id = 2, name = 'Carlo', city='333' WHERE id=<built-in function id>
0L
but the update query is wrong, what I want to obtain is something like:
UPDATE users SET name='Carlo', city='333' WHERE id='2'
There is a way to pass a dictionary in the db.update() function using a key/value as WHERE clause and the other key/value pairs as SET parameters?
the web.py documentation refer to the db.select() instructions but it's not clear at all to me..
Thanks for any help!
Federico
Upvotes: 1
Views: 1127
Reputation: 2198
Because id is a build in function, so you have to define it in vars:
db.update('users', where="id=$id", vars={'id': user['id']}, **user)
If you really don't want id=2 exists in update set, then you can do:
uid = user.pop('id')
db.update('users', where="id=$id", vars={'id': uid}, **user)
but remember to set it back if you still need it in user
Or just list every thing you want like this:
db.update('users', where="id=$id", vars={'id': uid}, name=user['name'], city=user['city'])
So vars will be used in where. All other named paramenter, except where, vars, _test will be in set
Upvotes: 1