AttributeError: 'NoneType' object has no attribute 'cursor' in flask and Mysql | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

AttributeError: 'NoneType' object has no attribute 'cursor' in flask and Mysql

I have worked with flask and mysql every thing working fine but due to some problem my system need to erase C drive data after that i try to install python and mysql and started working on it but i'm facing this error Python mysql code connection is working but flask and mysql is not working kindly help me

22nd Nov 2023, 11:45 AM
Jayashree M
5 Answers
0
This means there is a problem in the connection to database. First, make sure that Flask-MySQLDB is installed. pip install Flask-MySQLDB Then, make sure that mysql server is running. The last step, is to check the connection data, i.e. localhost, username, pass, and db info are all correct.
22nd Nov 2023, 12:22 PM
Abdulfattah Alhazmi
Abdulfattah Alhazmi - avatar
0
pip install Flask-MySQLDB --- Requirement satisfied This means there is a problem in the connection to database. no python and mysql connection is working well so putted same host,username,pass,db but same error i never faced this error occuring for this line cursor = mysql.connection.cursor()
22nd Nov 2023, 12:35 PM
Jayashree M
0
Yes, I see from the error that the issue you described above in the question suggests that the variable you're trying to access as a cursor is set to None. This can happen if there's an issue with your database connection. Please, make sure you rule out all issues related to db connection, before we can inveterate deeper. 1- Mysql server is running 2- Db parameters all are correct 3- Db user in the flask app have the all required privileges If those above are satisfied, please [Check Flask Application Setup] I mean, ensure that you're creating a MySQL connection object and using it to get a cursor. For example: from flask import Flask from flask_mysqldb import MySQL app = Flask(__name__) app.config['MYSQL_HOST'] = 'your_host' app.config['MYSQL_USER'] = 'your_user' app.config['MYSQL_PASSWORD'] = 'your_password' app.config['MYSQL_DB'] = 'your_database' mysql = MySQL(app)
22nd Nov 2023, 12:44 PM
Abdulfattah Alhazmi
Abdulfattah Alhazmi - avatar
0
i'll go through it Tq
22nd Nov 2023, 12:48 PM
Jayashree M
0
this code is working import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", password="abc@123#" ) mycursor = mydb.cursor() mycursor.execute("SHOW DATABASES") for x in mycursor: print(x) not working and get attribute error for cursor from flask import Flask from flask_mysqldb import MySQL app = Flask(__name__) app.config['MYSQL_HOST'] = 'localhost' app.config['MYSQL_USER'] = 'root' app.config['MYSQL_PASSWORD'] = 'abc@123#' app.config['MYSQL_DB'] = 'mydatabase' mysql = MySQL(app)
23rd Nov 2023, 4:58 AM
Jayashree M