如何使用 SQLALCHEMY 创建数据库?
how to create a database with SQLALCHEMY?
我想创建一个数据库来插入数据。我有一条错误消息。
根据文档,错误可能来自数据库本身。有人遇到过这个问题吗?
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError)
(Background on this error at: https://sqlalche.me/e/14/e3q8)
我的代码:
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
#configurer la connexion a la bdd
app.config["SQLALCHEMY_DATABASE_URI"] = 'postgresql://postgres:password@localhost:5432/data_collector'
db= SQLAlchemy(app)
class Data(db.Model):
__tablename__ = "data"
id=db.Column(db.Integer, primary_key=True)
email_=db.Column(db.String(120), unique=True)
height_=db.Column(db.Integer)
def __init__(self, email_, height_):
self.email_=email_
self.height_=height_
@app.route("/")
def index():
return render_template("index.html")
@app.route("/success", methods=['POST'])
def success():
if request.method == 'POST':
email = request.form["email_name"]
height = request.form["height_name"]
print(email)
print(height)
return render_template("success.html")
if __name__ == "__main__":
app.debug == True
app.run()
您的机器上似乎没有创建数据库 data_collector
。创建数据库后,您还可以将其包含在应用程序文件中:
if __name__ == "__main__":
db.create_all()
app.debug == True
app.run()
我想创建一个数据库来插入数据。我有一条错误消息。 根据文档,错误可能来自数据库本身。有人遇到过这个问题吗?
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError)
(Background on this error at: https://sqlalche.me/e/14/e3q8)
我的代码:
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
#configurer la connexion a la bdd
app.config["SQLALCHEMY_DATABASE_URI"] = 'postgresql://postgres:password@localhost:5432/data_collector'
db= SQLAlchemy(app)
class Data(db.Model):
__tablename__ = "data"
id=db.Column(db.Integer, primary_key=True)
email_=db.Column(db.String(120), unique=True)
height_=db.Column(db.Integer)
def __init__(self, email_, height_):
self.email_=email_
self.height_=height_
@app.route("/")
def index():
return render_template("index.html")
@app.route("/success", methods=['POST'])
def success():
if request.method == 'POST':
email = request.form["email_name"]
height = request.form["height_name"]
print(email)
print(height)
return render_template("success.html")
if __name__ == "__main__":
app.debug == True
app.run()
您的机器上似乎没有创建数据库 data_collector
。创建数据库后,您还可以将其包含在应用程序文件中:
if __name__ == "__main__":
db.create_all()
app.debug == True
app.run()