Python MySQL connection

How to start with Linux

Basic example of a MySQL connection using Python on Linux.

# Connect to a MySQL database
### Put the Database info in a seperate file called: "connection.py"###

# Database info
username = 'username'
password = 'password'
host = 'localhost'
db = 'your_db'

### In your main.py import the connection settings
import connection
# Import the MySQL connector module
import mysql.connector

conn = mysql.connector.connect(
  host=connection.host,
  user=connection.username,
  password=connection.password,
  database=connection.db
)

mycursor = conn.cursor()
mycursor.execute("SELECT * FROM cookies")
myresult = mycursor.fetchall()
for x in myresult:
  print(x)

# Close the connection
cursor.close()
conn.close()