Search code examples
pythonmysqlflaskfetchconnector

Trying to get data from MySQL


main.py

When I use cursor.execute.. outside of route it is working.

import mysql.connector
from flask import Flask, render_template, request, session, redirect, url_for, make_response

MySQL = mysql.connector.connect(
    user='root',
    password='password',
    host='localhost',
    database='dbname',
    use_pure=True
)
cursor = MySQL.cursor(prepared=True)

@app.route("/bridge", methods=["POST"])
def bridge():
    if request.get_json()["for"] == "signUp":
        if request.get_json()["type"] == "existence":

            cursor.execute("SELECT id FROM users WHERE eMail=%s", (request.get_json()["details"]["eMail"], ))
            print("----------------- {}".format(cursor.fetchone()))



    print(request.get_json()["details"]["eMail"])



    return make_response(json.dumps({"Res"}), 200)

cursor.close()
MySQL.close()

Error

cursor.execute("SELECT id FROM users WHERE eMail=%s", (request.get_json()["details"]["eMail"], )) File "C:\Users\woxro\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\mysql\connector\cursor.py", line 1186, in execute charset = self._connection.charset AttributeError: 'NoneType' object has no attribute 'charset'


Solution

  • The problem is that you create the connection and close it before bridge is called.see : MySQL.close()

    This is why it works outside of the bridge method