我有这个Python代码:
self.lock_tables("read", ['nets_permissions as n', 'devices_permissions as d'])
usrs = self.db.get("SELECT n.user_id FROM nets_permissions as n \
left join devices_permissions as d \
on n.user_id = d.user_id \
where d.user_id is null \
and n.network_id=%s and n.perm<>3", netid)
self.unlock_tables()
for usr in usrs:
self.lock_tables("write", ['devices_permissions'])
self.db.execute("INSERT devices_permissions SET \
user_id=%s, network_id=%s, device_id=%s, perm=%s",\
usr, netid, sensid, perm)
self.unlock_tables();
我首先执行查询以从两个表中检索一些user_id。我想将这个user_id保存在一个变量中,然后在执行for循环后将此记录插入另一个表...
此代码不起作用。我得到了这个错误:
异常:为Database.get()查询返回了多行
如何检索这多行,然后一次处理所有这些行?
谢谢大家。
答案 0 :(得分:0)
解决方案是使用 self.db.query 而不是 self.db.get !
self.lock_tables("read", ['nets_permissions as n', 'devices_permissions as d'])
usrs = self.db.query("SELECT n.user_id FROM nets_permissions as n \
left join devices_permissions as d \
on n.user_id = d.user_id \
where d.user_id is null \
and n.network_id=%s and n.perm<>3", netid)
self.unlock_tables()
for usr in usrs:
self.lock_tables("write", ['devices_permissions'])
self.db.execute("INSERT devices_permissions SET \
user_id=%s, network_id=%s, device_id=%s, perm=%s",\
usr['user_id'], netid, sensid, perm)
self.unlock_tables();