我正在尝试遵循本教程:
http://code.tutsplus.com/tutorials/creating-a-web-app-from-scratch-using-python-flask-and-mysql--cms-22972
我有Web应用程序出现,我可以在页面之间浏览,当我在填写详细信息后单击“注册”时,我在python中收到此错误:
ERROR:__main__:Exception on /signUp [POST]
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")
我不确定这里发生了什么,我在app.py根文件夹中有一个名为“BucketList.db”的数据库。
有没有办法找出被卡住的地方?或者为什么它无法连接到数据库?我可以使用Sqlite直接连接到数据库,这一切看起来都很好,所以可能是不正确的,它是如何通过localhost访问的?
任何帮助/指导将不胜感激!谢谢!
from flask import Flask, render_template, json, request
from flask_mysqldb import MySQL
from werkzeug import generate_password_hash, check_password_hash
mysql = MySQL()
app = Flask(__name__)
# MySQL configurations
app.config['MYSQL_DATABASE_DB'] = 'BucketList'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
app.config['MYSQL_DATABASE_PORT'] = '5002'
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():
try:
_name = request.form['inputName']
_email = request.form['inputEmail']
_password = request.form['inputPassword']
# validate the received values
if _name and _email and _password:
# All Good, let's call MySQL
conn = mysql.connect()
cursor = conn.cursor()
_hashed_password = generate_password_hash(_password)
cursor.callproc('sp_createUser',(_name,_email,_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>'})
except Exception as e:
return json.dumps({'error':str(e)})
finally:
cursor.close()
conn.close()
if __name__ == "__main__":
app.run(port=5002)
答案 0 :(得分:1)
您可以尝试避免在finally子句中再次连接。只需编写conn.close()和cursor.close()。它应该可以解决你的问题。
答案 1 :(得分:0)
这是我使用scrineym建议的Sqlite实现工作的代码。它的工作原理是你有一个进入并提交到数据库的应用程序。
现在我唯一的问题是,当我提交/获取错误/没有写入字段数据时,json部分无法正常工作!至少有一个解决方案适用于原始问题!
from flask import Flask, render_template, json, request
from werkzeug import generate_password_hash, check_password_hash
import sqlite3
from flask import g
DATABASE = 'BucketList.db'
#mysql = MySQL()
app = Flask(__name__)
@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():
_name = request.form['inputName']
_email = request.form['inputEmail']
_password = request.form['inputPassword']
# validate the received values
if _name and _email and _password:
print _name, _email
db = g._database = sqlite3.connect(DATABASE)
cursor = get_db().cursor()
print "Database opened"
_hashed_password = generate_password_hash(_password)
print _hashed_password
_userid = str(_name) + str(_hashed_password[0:4])
db.execute('INSERT INTO tbl_user VALUES (?,?,?,?)',(_userid,_name,_email,_hashed_password))
db.commit()
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>'})
cursor.close()
db.close()
print "Database closed"
"Print here"
if __name__ == "__main__":
app.run(port=5002)