Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
So i am trying to build a simple blog website where i am using Flask and flask_sqlalchemy.
Here i am trying to add the information ( Name,email id etc)
UI part
and then on button click , it will save to localhost/phpMyadmin.
Here it is Main.py file
from flask import Flask , render_template ,redirect, url_for, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost/PersonalBlog'
# initialize
db = SQLAlchemy(app)
# To Db
class PostTable(db.Model):
sno = db.Column(db.Integer, primary_key=True, unique=True, nullable=False)
date = db.Column(db.DATE, primary_key=False)
name = db.Column(db.String(120), unique=False, nullable=False)
emailid = db.Column(db.String(50), unique=False, nullable=False)
phone = db.Column(db.String(15), unique=False, nullable=False)
message = db.Column(db.String(120), unique=False, nullable=False)
@app.route("/")
def home():
return render_template('index.html')
@app.route("/homeclick")
def homeclick():
return render_template('index.html')
@app.route("/about")
def about():
return render_template('about.html')
@app.route("/post")
def post():
return render_template('post.html')
@app.route("/contact" , methods=['POST', 'GET'])
def contact():
if request.method == 'POST' :
# add entry to the database
name = request.form.get('name')
emailid = request.form.get('email')
phone = request.form.get('phone_number')
message = request.form.get('message')
entry_to_db = PostTable(name=name, emailid=emailid , phone=phone, message=message)
db.session.add(entry_to_db)
db.session.commit()
return render_template('contact.html')
if __name__ == '__main__':
app.run(debug=True, port=4013 , threaded=True)
The following error is getting displayed when i click on Submit button after giving details.
sqlalchemy.exc.OperationalError
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1045, "Access denied for user 'root'@'localhost' (using password: NO)")
(Background on this error at: http://sqlalche.me/e/14/e3q8)
Full Traceback Details if it helps
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 3211, in _wrap_pool_connect
return fn()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 307, in connect
return _ConnectionFairy._checkout(self)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 767, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 425, in checkout
rec = pool._do_get()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 146, in do_get
self.dec_overflow()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 70, in exit
compat.raise(
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise
raise exception
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get
return self._create_connection()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 253, in _create_connection
return _ConnectionRecord(self)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 368, in init
self.__connect()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 611, in connect
pool.logger.debug("Error on connect(): %s", e)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 70, in exit
compat.raise(
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise
raise exception
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 605, in __connect
connection = pool._invoke_creator(self)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/engine/create.py", line 578, in connect
return dialect.connect(*cargs, **cparams)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 584, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/MySQLdb/init.py", line 130, in Connect
return Connection(*args, **kwargs)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/MySQLdb/connections.py", line 185, in init
super().init(*args, **kwargs2)
The above exception was the direct cause of the following exception:
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/flask/app.py", line 2088, in call
return self.wsgi_app(environ, start_response)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.handle_exception(e)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/flask/app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/flask/app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/flask/app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/flask/app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/main.py", line 48, in contact
db.session.commit()
File "", line 2, in commit
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1428, in commit
self._transaction.commit(_to_root=self.future)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 829, in commit
self._prepare_impl()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 808, in _prepare_impl
self.session.flush()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 3298, in flush
self._flush(objects)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 3438, in flush
transaction.rollback(capture_exception=True)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 70, in exit
compat.raise(
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise
raise exception
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 3398, in _flush
flush_context.execute()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 456, in execute
rec.execute(self)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 630, in execute
util.preloaded.orm_persistence.save_obj(
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 209, in save_obj
for (
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 370, in organize_states_for_save
for state, dict, mapper, connection in _connections_for_states(
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 1709, in _connections_for_states
connection = uowtransaction.transaction.connection(base_mapper)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 626, in connection
return self._connection_for_bind(bind, execution_options)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 735, in _connection_for_bind
conn = self._parent._connection_for_bind(bind, execution_options)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 747, in _connection_for_bind
conn = bind.connect()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 3165, in connect
return self._connection_cls(self, close_with_result=close_with_result)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 96, in init
else engine.raw_connection()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 3244, in raw_connection
return self._wrap_pool_connect(self.pool.connect, _connection)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 3214, in _wrap_pool_connect
Connection.handle_dbapi_exception_noconnection(
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2068, in handle_dbapi_exception_noconnection
util.raise(
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise
raise exception
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 3211, in _wrap_pool_connect
return fn()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 307, in connect
return _ConnectionFairy._checkout(self)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 767, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 425, in checkout
rec = pool._do_get()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 146, in do_get
self.dec_overflow()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 70, in exit
compat.raise(
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise
raise exception
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get
return self._create_connection()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 253, in _create_connection
return _ConnectionRecord(self)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 368, in init
self.__connect()
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 611, in connect
pool.logger.debug("Error on connect(): %s", e)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 70, in exit
compat.raise(
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise
raise exception
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 605, in __connect
connection = pool._invoke_creator(self)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/engine/create.py", line 578, in connect
return dialect.connect(*cargs, **cparams)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 584, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/MySQLdb/init.py", line 130, in Connect
return Connection(*args, **kwargs)
File "/Users/ranjankumar/Desktop/Folders/Project Flask/env/lib/python3.8/site-packages/MySQLdb/connections.py", line 185, in init
super().init(*args, **kwargs2)
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1045, "Access denied for user 'root'@'localhost' (using password: NO)")
(Background on this error at: http://sqlalche.me/e/14/e3q8)
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.
You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:
dump() shows all variables in the frame
dump(obj) dumps all that's known about the object
Your connection string is not using a passoword, mysql://root:<PASSWORD>@localhost/PersonalBlog
You should try to login to your DB without password using root as user and see what happens.
You can try adding a new user with password which has full (or not) access to that DB, so you just change the user root
in your case and <PASSWORD>
to the new ones. You can also try changing/setting a password to the root user.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.