function.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from connect import connect
  2. conn = connect()
  3. def query_select(query, args=(), one=False):
  4. try:
  5. with conn.cursor() as cursor:
  6. cursor.execute(query, args)
  7. r = [dict((cursor.description[i][0], value) \
  8. for i, value in enumerate(row)) for row in cursor.fetchall()]
  9. return (r[0] if r else None) if one else r
  10. except:
  11. return "Error"
  12. def query_insert(query, args=()):
  13. try:
  14. with conn.cursor() as cursor:
  15. cursor.execute(query, args)
  16. #id = cursor.fetchone()[0]
  17. conn.commit()
  18. return cursor.fetchone()[0]
  19. except:
  20. return "Error"
  21. def query_delete(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_update(query, args=()):
  32. try:
  33. with conn.cursor() as cursor:
  34. cursor.execute(query, args)
  35. print(cursor.rowcount)
  36. if(cursor.rowcount != 0):
  37. conn.commit()
  38. return True
  39. return False
  40. except:
  41. return False