[英]Flask SQLAlchemy Connect to MySQL Database
I'm building a basic Flask Application with an an AngularJS front end and I am currently at the point where I need to make a connection to a MySQL database I have hosted with Godaddy phpmyadmin. 我正在使用AngularJS前端构建基本的Flask应用程序,目前我需要连接到由Godaddy phpmyadmin托管的MySQL数据库。
This is part of my __init__.py
这是我的
__init__.py
一部分
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
# Create instnace called app
app = Flask(__name__)
app.config['SQLAlchemy_DATABASE_URI'] = 'mysql://username:password#@xxxxxx.hostedresource.com/dbname'
# Create SQLAlchemy object
db = SQLAlchemy(app)
# ...
This is my models.py
这是我的
models.py
from app import app, db
class UsersPy(db.Model):
__tablename__ = "userspy"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String, nullable=False)
password = db.Column(db.String, nullable=False)
def __init__(self, username, password):
self.username = username
self.password = password
def __repr__(self):
return '<title {}'.format(self.username)
This is a snippet from my views.py: 这是我的views.py的一个片段:
from app import app, db
from app.models import UsersPy
from flask import render_template, request, redirect, url_for, jsonify, session, flash
@app.route('/testdb/')
def testdb():
admin = UsersPy('user1', 'password1')
guest = UsersPy('user2', 'password2')
db.session.add(admin)
db.session.add(guest)
#db.session.merge(admin)
#db.session.merge(guest)
db.session.commit()
results = UsersPy.query.all()
json_results = []
for result in results:
d = {'username': result.username,
'password': result.password}
json_results.append(d)
return jsonify(items=json_results)
All of this works well, the users are 'created' and displayed back in JSON format when you visit the /testdb/ location, however the actual database hosted with Godaddy is not being updated so the real connection must not be being made or it is failing for some reason. 所有这一切都很好,当您访问/ testdb /位置时,将“创建”用户并以JSON格式显示用户,但是Godaddy托管的实际数据库没有更新,因此不能建立真正的连接,或者由于某种原因而失败。 I have the userspy database table already created but add() and commit() functions are not actually adding users to the database.
我已经创建了userspy数据库表,但是add()和commit()函数实际上并未将用户添加到数据库中。 I can't figure out how to solidify the connection between SQLAlchemy and the MySQL Database.
我不知道如何巩固SQLAlchemy和MySQL数据库之间的连接。 Any help is appreciated, thanks.
任何帮助表示赞赏,谢谢。
A good first step in debugging this would be to set SQLALCHEMY_ECHO
to True
in your app configuration. 调试此程序的一个很好的第一步是在应用程序配置中将
SQLALCHEMY_ECHO
设置为True
。 This will cause the actual MySQL database statements that SQLAlchemy is executing to be printed out. 这将导致打印出SQLAlchemy正在执行的实际MySQL数据库语句。
Also, it's worth noting that SQLAlchemy_DATABASE_URI
is different from SQLALCHEMY_DATABASE_URI
, which is what the Flask-SQLAlchemy documentation states the config key should be. 另外,值得注意的是,
SQLAlchemy_DATABASE_URI
与SQLALCHEMY_DATABASE_URI
不同,这是Flask-SQLAlchemy文档指出的配置键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.