I have looked everywhere and well researched into different solutions. I have come closer to the solution by reducing the number of errors. Here is my piece of code that's giving me a headache
from flask import Flask, render_template, json, request
from flaskext.mysql import MySQL
from werkzeug import generate_password_hash,check_password_hash
mysql = MySQL()
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'root'
app.config['MYSQL_DB'] = 'BucketList'
mysql.init_app(app)
cur = mysql.connection.cursor()
I have been running the app.py file and keep getting the following error:
AttributeError: 'MySQL' object has no attribute 'connection'.
I am using MySQL 5.7 and attempting to create my first web app using a tutorial that's dated back in time. The above piece of code is after editing it closer to relevant standards.
The part of your code in which you instantiate the MySQl() object is incorrect. Instead of:
mysql = MySQL()
app = Flask(__name__)
It should be
app = Flask(__name__)
mysql = MySQL(app)
Related
I am trying to connect my flask application to a DB2 database,but somehow I am unable to do. here is what I am trying to do,
I am using flask extension flask_db2 for this purpose and below is how I am trying to connect
from flask import Flask
from flask_db2 import DB2
app = Flask(__name__)
#app.route('/')
def index()
cur = db.connection.cursor("DATABASE=name;HOSTNAME=host;PORT=60000;PROTOCOL=TCPIP;UID=username;PWD=password)
from flask import Flask
from flask_db2 import DB2
app = Flask(__name__)
app.config['DB2_DATABASE'] = 'sample'
app.config['DB2_HOSTNAME'] = 'localhost'
app.config['DB2_PORT'] = 50000
app.config['DB2_PROTOCOL'] = 'TCPIP'
app.config['DB2_USER'] = 'db2inst1'
app.config['DB2_PASSWORD'] = 'db2inst1'
db = DB2(app) #You forgot that
#app.route('/')
def index():
cur = db.connection.cursor()
cur.execute(...)
First: You forgot to have an object to use the flask db2 module.
The DB2(app) is a constructor for your Flask extension and will bind your application to this module.
where as
db = DB2(app)
Initialises your db object with help of your extension's constructor which you can further use for your Database operations.
Secondly: use app configs to play with DB2 credentials. (This step should be done before you bind the app)
Third: You can execute your query like this
cur.execute("SELECT id FROM users") #something like that
I hope I have clarified :D just drop message. Ill try helping more.
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'] = 'password'
app.config['MYSQL_DB'] = 'todoapp'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql = MySQL(app)
cur = mysql.connection.cursor()
if __name__ == '__main__':
app.run()
There is error which is displayed after executing program:
cur = mysql.connection.cursor()
AttributeError: 'NoneType' object has no attribute 'cursor'.
According to documentaction it should work. I use Ubuntu 16.04, I have installed MySQL and it works properly. Could anyone explain why it doesn't work?
You are constructing cursor based on flask_mysqldb , and Flask app won't be constructed itself up until the first route is hit, which means the Flask app will be constructed inside a Flask Function, and that is when your MySQL connection also can be constructed based on your app.config params, and then your cursor can be constructed based on MySQL connection:
Flask Construction > MySQL Connection Construction > Cursor Construction.
So you have to use your cursor constructor inside a Flask Function:
Instead of:
cur = mysql.connection.cursor()
Put:
#app.route("/")
def index():
cur = mysql.connection.cursor()
I used the connect() method instead of get_db() and it works with Python 3.5.5. I had the same error when I used get_db
from flask import Flask
from flaskext.mysql import MySQL
app = Flask(__name__)
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'test_db'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql = MySQL()
mysql.init_app(app)
# cursor = mysql.get_db().cursor()
cursor = mysql.connect().cursor()
print(cursor)
The problem is in :
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
Looking at flaskext.mysql source , that feature is not yet implemented .
And since flaskext.mysql is using pymysql , you can use DictCursor from pymysql.cursors
Example :
from flaskext.mysql import MySQL
from flask import Flask
from pymysql import cursors
app=Flask(__name__)
mysql = MySQL(cursorclass=cursors.DictCursor)
mysql.init_app(app)
cursor = mysql.connect().cursor()
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_DATABASE_PORT'] = 3308 #here your port
It maybe that you need to init the app for MySQL context.
from flask import Flask
from flask_mysqldb import MySQL
app = Flask(__name__)
mysql = MySQL()
mysql.config['MYSQL_HOST'] = 'localhost'
mysql.config['MYSQL_USER'] = 'root'
mysql.config['MYSQL_PASSWORD'] = 'password'
mysql.config['MYSQL_DB'] = 'todoapp'
mysql.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql.init_app(app)
cur = mysql.connection.cursor()
if __name__ == '__main__':
app.run()
I'm trying to establish a connection to an sql database, but I end up with
AttributeError: 'MySQL' object has no attribute 'connection'
I've read through the documentation, and it looks like this is the proper way to create a cursor:
from flask import Flask
from flaskext.mysql import MySQL
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'sql3.freemysqlhosting.net'
app.config['MYSQL_USER'] = 'sql1234567'
app.config['MYSQL_PASSWORD'] = 'abcdefg'
app.config['MYSQL_DB'] = 'sql1234567'
app.config['MYSQL_PORT'] = '3306'
mysql = MySQL(app)
#app.route('/')
def index():
cur = mysql.connection.cursor()
cur.execute('''SELECT * FROM users''')
rv = cur.fetchall()
return str(rv)
if __name__ == '__main__':
app.run(debug=True)
Edit: using https://www.youtube.com/watch?v=nWO_VpI5wn8 as a tutorial and the docs are http://flask-mysqldb.readthedocs.io/en/latest/
You are importing incorrect module here.
Solution is
Instead of importing
from flaskext.mysql import MySQL
import this which is different module (Follow this)
from flask_mysqldb import MySQL
This looks like an auto import issue from some IDE. As these two modules are different, you can install these two packages as following:
pip install flask-mysqldb and
pip install flask-mysql
EDIT
If you want to use the previous module, you will have to follow this and the cursor can be used as following:
from flaskext.mysql import MySQL
mysql = MySQL()
mysql.init_app(app)
cursor = mysql.get_db().cursor()
So I have main .py file where Flask app object is created and configured, and MySQL is initialized. Then I want to register some blueprint.
from flask import Flask
from flaskext.mysql import MySQL
app = Flask(__name__)
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'EmpData'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
from views1 import views1_blprnt
app.register_blueprint(views1_blprnt)
But in views1.py I need MySQL object to get connection and cursor objects to execute queries. And, of course, when I try to import it I get the ImportError. I've read some similar questions and workarounds, but all of them were using SQLAlchemy. Does someone have any ideas how to resolve it?
Thank you for your help and sorry for my english.
A fairly typical pattern here is to use a third module where you initialize mysql and any other resources you want to share among your blueprints.
I've noticed that people tend to call it extensions.py. So you might do something like this in extension.py:
from flaskext.mysql import MySQL
mysql = MySQL()
And in your main.py, something like this:
from extensions import mysql
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'EmpData'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
You'd access the initialized mysql instance from all your blueprints like so:
from extensions import mysql
mysql....
There are other solutions, but this is what I see done most often. It's what I usually do as well.
I am using sqlalchemy with flask
and I want to connect to oracle DB as sysdba
SQLALCHEMY_DATABASE_URI ='oracle+cx_oracle://sys:abc#DBNAME[mode=SYSDBA]'
This doesnt work and gives me a
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
from app import views,models
and I use this db object later. But I am not able to figure out how to write the
SQLALCHEMY_DATABASE_URI to login as sysdba
I also tried
CONN = cx_Oracle.connect('sys/abc', dsn='DBNAME', mode = cx_Oracle.SYSDBA)
SQLALCHEMY_DATABASE_URI = CONN
But that also doesnt work.
I get ORA-12154: TNS: could not resolve the connect identifier specified” .. also If I remove mode=SYSDBA I get ORA-28009 connection as SYS should be as SYSDBA
Your dsn parameter is wrong. You must also separate the user and password parameters. Try this (it's working for me):
dsn_tns = cx_Oracle.makedsn('host', port, 'sid')
CONN = cx_Oracle.connect('sys', 'abc', dsn_tns, mode=cx_Oracle.SYSDBA)
For more info see cx_Oracle.connect constructor.