Replace MariaDB connection with Flask-MySQLdb integration

This commit is contained in:
ishak jmilou.ishak
2024-12-09 14:59:42 +01:00
parent 9689d70104
commit 3bb40d5929

View File

@@ -1,6 +1,6 @@
from flask import Flask, request, render_template, jsonify from flask import Flask, request, render_template, jsonify
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
import mariadb from flask_mysqldb import MySQL
app = Flask(__name__) app = Flask(__name__)
@@ -18,12 +18,12 @@ mqtt_client.loop_start()
mqtt_client.subscribe("kobuki/data") mqtt_client.subscribe("kobuki/data")
mqtt_client.on_message = on_message # this lines needs to be under the function definition otherwise it cant find which function it needs to use mqtt_client.on_message = on_message # this lines needs to be under the function definition otherwise it cant find which function it needs to use
conn = mariadb.connect( app.config['MYSQL_HOST'] = 'localhost'
host='127.0.0.1', app.config["MYSQL_USER"] = 'admin'
user='admin', app.config["MYSQL_PASSWORD"] = 'kobuki'
password='kobuki', app.config["MYSQL_DB"] = 'kobuki'
database='kobuki')
cur = conn.cursor() mysql = MySQL(app)
@app.route('/') @app.route('/')
@@ -61,7 +61,8 @@ def phpmyadmin_passthrough(path):
@app.route("/database") @app.route("/database")
def database(): def database():
return "Connected to database" cur = mysql.connection.cursor()
cur.execute("SELECT * FROM kobuki")
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True, port=5000) app.run(debug=True, port=5000)