This Python Code Outputs its Data to a MySQL Database

This code creates a connection to a MySQL database and uses a cursor object to execute an INSERT statement that inserts the values of the number_1, number_2, and sum variables into a table called numbers. It then commits the transaction and closes the cursor and connection. Finally, it sends the two numbers and the sum to Airtable using the webhook, and checks the status code of the response to see if the request was successful.

Remember to replace the placeholders in the mysql.connector.connect() function with the actual hostname, username, password, and database name for your MySQL database.

import requests
import mysql.connector

# Replace YOUR_WEBHOOK_URL with the actual webhook URL from Airtable
webhook_url = "YOUR_WEBHOOK_URL"

# Prompt the user for the first number
number_1 = int(input("Enter the first number: "))

# Prompt the user for the second number
number_2 = int(input("Enter the second number: "))

# Calculate the sum of the two numbers
sum = number_1 + number_2

# Connect to the MySQL database
conn = mysql.connector.connect(
  host="hostname",
  user="username",
  password="password",
  database="database_name"
)

# Create a cursor object
cursor = conn.cursor()

# Execute an INSERT statement to insert the values into the database
cursor.execute("INSERT INTO numbers (number_1, number_2, sum) VALUES (%s, %s, %s)", (number_1, number_2, sum))

# Commit the transaction
conn.commit()

# Send the two numbers and the sum to Airtable using the webhook
response = requests.post(webhook_url, json={"number_1": number_1, "number_2": number_2, "sum": sum})

# Check the status code of the response to see if the request was successful
if response.status_code == 200:
  print("Successfully sent numbers and sum to Airtable and MySQL")
else:
  print("Failed to send numbers and sum to Airtable")

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

 

Leave the first comment