Okay, so I was definitely doing the unicode and joining wrong. New version
below. I *think* this is correct. (It definitely works now, at least.)
-----
def multiple_update(table, where, values_vars, database=None, _test=False):
r"""Execute multiple separate update statements in one query.
>>> database = web.DB(None, {})
>>> row1 = ({'name': 'Bob'}, {'id': 42})
>>> row2 = ({'name': 'Sarah', 'age': 30}, {'id': 23})
>>> query = multiple_update('foo', 'id = $id', [row1, row2],
_test=True, database=database)
>>> query
<sql: "UPDATE foo SET name = 'Bob' WHERE id = 42;\nUPDATE foo SET age =
30, name = 'Sarah' WHERE id = 23">
>>> query.query()
'UPDATE foo SET name = %s WHERE id = %s;\nUPDATE foo SET age = %s, name
= %s WHERE id = %s'
>>> query.values()
['Bob', 42, 30, 'Sarah', 23]
>>> query = multiple_update('bar', 'a = $b', [({'c':
decimal.Decimal(2)}, {'b': 3})], _test=True, database=database)
>>> query
<sql: "UPDATE bar SET c = Decimal('2') WHERE a = 3">
>>> query.query()
'UPDATE bar SET c = %s WHERE a = %s'
>>> query.values()
[Decimal('2'), 3]
>> print multiple_update('foo', 'id = $id', [], _test=True,
database=database)
None
"""
if not values_vars:
return
if database is None:
database = dbconn.db_connection
updates = []
for values, vars in values_vars:
updates.append(
'UPDATE ' + table +
' SET ' + web.db.sqlwhere(values, ', ') +
' WHERE ' + database._where(where, vars)
)
query = web.SQLQuery.join(updates, sep=';\n')
if _test:
return query
database.query(query)
-----
-Ben
--
You received this message because you are subscribed to the Google Groups
"web.py" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/webpy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.