Question

I have this Python code:

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();

I first do a query to retrieve some user_id from two tables. I want save this user_id in one variable and after do a for loop to insert this records in another table...

This code doesn't work. I obtain this error:

Exception: Multiple rows returned for Database.get() query

How can I retrieve this multiple rows and then process everyone of them at one time?

Thank you all.

Was it helpful?

Solution

The solution is to use self.db.query instead of 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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top