Hello, I am trying to connect to a MySQL-database but have no success. Code runs per Flask in a web app, no virtual env involved. The entry in the server log reads: name 'MySQLdb' is not defined. Credentials should be OK, since entering them in Python-Shell connection could be established. The import of MySQLdb is set in a try/except-clause, since formerly messages claimed uncaught exception. With or without clause result has same outcome. Relevant code reads:
#### module which receives the sensor data and stores it in a database
from flask import Flask
try:
import MySQLdb
except Exception as e:
print("Import MySQLdb failed .....", e)
def checkDBConnection():
try:
db = MySQLdb(app)
cnx = db.connect(
host = '<userName>.mysql.eu.pythonanywhere-services.com',
user = '<userName>',
password = '*******',
database = '<userName>$sensorData'
)
if cnx.is_connected():
return True
else:
return False
except Exception as e:
print("Database connection error: ", e)
return False
app = Flask(__name__)
#application = Flask(__name__) # see hint in *.wsgi file
@app.route("/sensorData/<val>")
def storeDB(val):
return f'val is: {val}'
@app.route("/accDB")
def accessDB():
check = checkDBConnection()
if check:
return "<p>Database access successful...........</p>"
# closing database connection
#cnx.close()
else:
return "<p>Connection failed ......</p>"
Would be glad, if one could help. Thank you!