I have this python code in Tornado:
if not usr:
self.lock_tables("write", ['devices_permissions'])
self.db.execute("INSERT devices_permissions SET user_id=%s, network_id=%s, device_id=%s, perm=%s",
usrid, netid, sensid, perm)
self.unlock_tables();
else:
if perm == 0:
self.lock_tables("write", ['devices_permissions'])
self.db.execute("DELETE FROM devices_permissions \
WHERE user_id=%s AND device_id=%s", usrid, sensid)
self.unlock_tables();
else:
self.lock_tables("write", ['devices_permissions'])
self.db.execute("UPDATE devices_permissions SET perm=%s \
WHERE user_id=%s AND device_id=%s", perm, usrid, sensid)
self.unlock_tables();
I have a problem with the second if…else statement. My program enter in the first else block, but in the second if…else seems not to work the if perm == 0 statement. I have print the perm variable and its value is 0. So I don’t know where sould be the error.
I want this if…else statement because I want delete from my DB the users that didn’t have anymore a permission on a device.
perm is a variable of value 0 or 1. usr is a list of user. Thank you very much.
EDIT
I change the statement in this way:
# Check whether the user has already priviledges on the device
self.lock_tables("read", ['devices_permissions'])
usr = self.db.get("SELECT device_id, user_id FROM devices_permissions \
WHERE user_id=%s AND device_id=%s", usrid, sensid)
self.unlock_tables();
if not usr:
self.lock_tables("write", ['devices_permissions'])
self.db.execute("INSERT devices_permissions SET user_id=%s, network_id=%s, device_id=%s, perm=%s",
usrid, netid, sensid, perm)
self.unlock_tables();
elif perm == 0:
self.lock_tables("write", ['devices_permissions'])
self.db.execute("DELETE FROM devices_permissions \
WHERE user_id=%s AND device_id=%s", usrid, sensid)
self.unlock_tables();
else:
self.lock_tables("write", ['devices_permissions'])
self.db.execute("UPDATE devices_permissions SET perm=%s \
WHERE user_id=%s AND device_id=%s", perm, usrid, sensid)
self.unlock_tables();
and after some test I think that the program enter always in the if not usr statement, also if the usr list is not empty. What’s the problem?
Could it be that
permis the string zero rather than an integer zero? If so, the testperm == 0would fail. You can answer this question with a print statement:if it prints
<type 'str'>then that’s your problem.