0

Hi I'm following a tutorial online to build a flask web app and keeping coming across this error when I try to store information into my db via the sign up button.

from flask import Flask, render_template, json, request from flask.ext.mysqldb import MySQL from werkzeug import generate_password_hash, check_password_hash app = Flask(__name__) mysql = MySQL(app) # MySQL configurations app.config['MYSQL_DATABASE_USER'] = 'root' app.config['MYSQL_DATABASE_PASSWORD'] = 'pass' app.config['MYSQL_DATABASE_DB'] = 'table' app.config['MYSQL_DATABASE_HOST'] = 'localhost' mysql.init_app(app) @app.route("/") def main(): return render_template('index.html') @app.route('/showSignUp') def showSignUp(): return render_template('signup.html') @app.route('/signUp', methods = ['POST','GET' ]) def signUp(): _phonenumber = request.form['phonenumber'] _name = request.form['name'] _password = request.form['password'] if _phonenumber and _name and _password: conn = mysql.connect() cursor = conn.cursor() _hashed_password = generate_password_hash(_password) cursor.callproc('sp_createphoneuser', (_phonenumber,_name,_hashed_password)) data = cursor.fetchall() if len(data) is 0: conn.commit() return json.dumps({'message':"User created successfully !"}) else: return json.dumps({'error':str(data[0])}) else: return json.dumps({'html':'<span>Enter the required fields</span>'}) if __name__ == "__main__": app.debug = True app.run(port=5002) 

So that is the code I have and everytime I try to register a user I get this error in terminal:

File "/home/john/FlaskApp/app.py", line 35, in signUp conn = mysql.connect() TypeError: 'Connection' object is not callable 

My theory is that there was a problem during installation but I'm not sure.

1
  • 1
    Please check that the indentation in your code is correct. Commented Aug 20, 2016 at 6:37

2 Answers 2

6

I faced the same problem recently and I solved it

by using conn = mysql.connect instead of conn = mysql.connect()

Sign up to request clarification or add additional context in comments.

Comments

1

That's not how you get a connection, or a cursor, with flask.ext.mysqldb. See the documentation.

It should be:

cur = mysql.connection.cursor() 

so you don't need to explicitly create a connection at all.

(Note that there does seem to be a separate project, flask-mysql, with a slightly different API - that could be where the confusion is coming from.)

1 Comment

Thanks for the help but now I'm getting this error: _mysql_exceptions.OperationalError: (1046, 'No database selected')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.