Reputation: 35
I have a python3 file:
from flask import Flask, request
import mysql.connector
db = mysql.connector.connect(
host="*host*",
user="*user*",
password="*password*",
database="*database*"
)
cursor = db.cursor()
app = Flask(__name__)
@app.errorhandler(404)
def page_not_found(e):
return "<h1>404</h1><p>The resource could not be found.</p>", 404
@app.route('/<date>/<temperature>', methods=['POST'])
def index(date, temperature):
try:
cursor.execute(f"INSERT INTO temperature VALUES ('{date}', {temperature})")
db.commit()
except mysql.connector.Error as error:
print(f"Failed to insert data into MySQL table: {error}")
db.rollback()
return "Data inserted successfully"
@app.route('/addtemperature/', methods=['POST'])
def addtemperature(date, temperature):
data = request.json()
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=5000 )
and when I run it using:
python3 app.py
I get a error saying:
Traceback (most recent call last):
File "/Users/daniel/Desktop/Python3/Raspberry Pi Pico/app.py", line 2, in <module>
import mysql.connector
ModuleNotFoundError: No module named 'mysql'
I am using anaconda and I have already installed mysql-connector-python. I have also tried uninstalling then reinstalling. I am using MacOS 15.1.1 (Sequoia)
Upvotes: 0
Views: 39