Guest User

Untitled

a guest
Dec 10th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. """
  2. To connect to mariadb you would need to conda install the mysql-connector-python package
  3.  
  4. conda install -c anaconda mysql-connector-python
  5. """
  6. import mysql.connector as mariadb
  7. import json
  8.  
  9.  
  10. # Read the credentials from secret
  11. credentials = None
  12. with open('/var/run/secrets/user_credentials/mariadb_credentials') as f:
  13. credentials = json.load(f)
  14.  
  15. # Ensure your credentials were setup
  16. if credentials:
  17. # Connect to the DB
  18. connection = mariadb.connect(
  19. user=credentials.get('user'),
  20. password=credentials.get('password'),
  21. database=credentials.get('database'),
  22. host=credentials.get('host')
  23. )
  24. cursor = connection.cursor()
  25.  
  26. # Execute the query
  27. cursor.execute("SELECT first_name, last_name FROM employees LIMIT 20")
  28.  
  29. # Loop through the results
  30. for first_name, last_name in cursor:
  31. print(f'First name: {first_name}, Last name: {last_name}')
  32.  
  33. # Close the connection
  34. connection.close()
Add Comment
Please, Sign In to add comment