database.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from mysql.connector import connect
  2. import config as conf
  3. def sql_check_user(db_query):
  4. conn = connect(host=conf.db_host,
  5. user=conf.db_user,
  6. password=conf.db_password,
  7. database=conf.db_name)
  8. cursor = conn.cursor()
  9. cursor.execute(db_query)
  10. result_set = cursor.fetchall()
  11. if len(result_set) == 0:
  12. return False
  13. elif len(result_set) > 0:
  14. return True
  15. def sql_simple_check(db_query, field):
  16. conn = connect(host=conf.db_host,
  17. user=conf.db_user,
  18. password=conf.db_password,
  19. database=conf.db_name)
  20. cursor = conn.cursor(dictionary=True)
  21. cursor.execute(db_query)
  22. result_set = cursor.fetchone()
  23. if result_set[field] == '0':
  24. return False
  25. else:
  26. return True
  27. def sql_parse_users(db_query):
  28. conn = connect(host=conf.db_host,
  29. user=conf.db_user,
  30. password=conf.db_password,
  31. database=conf.db_name)
  32. cursor = conn.cursor(dictionary=True)
  33. cursor.execute(db_query)
  34. result_set = cursor.fetchall()
  35. # print(len(result_set))
  36. users_list = []
  37. if len(result_set) == 0:
  38. return False
  39. elif len(result_set) > 0:
  40. for row in result_set:
  41. users_data = {"ID": row['id'],
  42. "ФИО": row['name'],
  43. "Номер телефона": row['phone']}
  44. users_list.append(users_data)
  45. return users_list
  46. def sql_query_send(db_query):
  47. conn = connect(host=conf.db_host,
  48. user=conf.db_user,
  49. password=conf.db_password,
  50. database=conf.db_name)
  51. cursor = conn.cursor()
  52. cursor.execute(db_query)
  53. conn.commit()
  54. conn.close()
  55. # sql_query_users(f"select name,phone from user_table where approved = '0'")
  56. # print(sql_simple_check(f"select approved from user_table where tg_id='338836490'"))
  57. # print(sql_parse_users(f"select id, name,phone from user_table where approved = '0'"))
  58. # print(sql_simple_check(f"select admin from user_table where tg_id = '338836490'", "admin"))
  59. # print(not not sql_query_single_get(f"select tg_id from user_table where tg_id = '11'"))
  60. # sql_query(f"INSERT INTO user_table (tg_id,name,phone) VALUES ('123123','ФФ ыв ывыв','89539299323')")
  61. # select name,phone from user_table where approved = '0'