function.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from connect import connect
  2. conn = connect()
  3. class RestFunc:
  4. def query_select(self, query, args=(), one=False):
  5. try:
  6. with conn.cursor() as cursor:
  7. cursor.execute(query, args)
  8. r = [dict((cursor.description[i][0], value) \
  9. for i, value in enumerate(row)) for row in cursor.fetchall()]
  10. return (r[0] if r else None) if one else r
  11. except Exception as ex:
  12. return {"msg":str(ex)}
  13. def query_insert(self, query, args=()):
  14. try:
  15. with conn.cursor() as cursor:
  16. cursor.execute(query, args)
  17. conn.commit()
  18. return cursor.fetchone()[0]
  19. except Exception as ex:
  20. return {"msg":str(ex)}
  21. def query_delete_update(self, query, args=()):
  22. try:
  23. with conn.cursor() as cursor:
  24. cursor.execute(query, args)
  25. if(cursor.rowcount != 0):
  26. conn.commit()
  27. return True
  28. return False
  29. except:
  30. return False
  31. def query_proverka(self, query, args=()):
  32. try:
  33. with conn.cursor() as cursor:
  34. cursor.execute(query, args)
  35. if(cursor.rowcount > 0):
  36. return True
  37. return False
  38. except:
  39. return False