Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. def connect_to_db():
  2. try:
  3. #connect to database
  4. url = urllib.parse.urlparse(os.environ["DATABASE_URL"])
  5.  
  6. conn = psycopg2.connect(
  7. database=url.path[1:],
  8. user=url.username,
  9. password=url.password,
  10. host=url.hostname,
  11. port=url.port
  12. )
  13.  
  14. print('connected to db\n')
  15. return [conn, conn.cursor()]
  16.  
  17. except Exception as e:
  18. print("err on connect_db: " + str(e))
  19.  
  20. def disconnect_db(conn, cursor):
  21. try:
  22. cursor.close()
  23. conn.close()
  24. except Exception as e:
  25. print("err on disconnect_db: " + str(e))
  26. return False
  27. return True
  28.  
  29. def get_id_from_username(username):
  30. try:
  31. [conn, cur] = connect_to_db()
  32. cur.execute("SELECT chat_id FROM players WHERE username = %s", (username,))
  33. temp = cur.fetchone()[0]
  34. disconnect_db(conn, cur)
  35. return temp
  36. except Exception as e:
  37. print("err on get id from username: " + format(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement