0
我按照教程創建了一個簡單的數據庫支持的網站。 Here如何創建更多表Pythonanywhere
我成功地遵循了教程,但我正在努力使用此方法創建多個表。我希望桌子被稱爲成分。 這裏是flask_app.py
from flask import Flask, redirect, render_template, request, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config["DEBUG"] = True
SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:
{password}@{hostname}/{databasename}".format(
username="Harrryj",
password="mypassword",
hostname="Harrryj.mysql.pythonanywhere-services.com",
databasename="Harrryj$comments",
)
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
class Comment(db.Model):
__tablename__ = "Comment"
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(100))
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "GET":
return render_template("main_page.html",
comments=Comment.query.all())
comment = Comment(content=request.form["contents"])
db.session.add(comment)
db.session.commit()
return redirect(url_for('index'))
class Ingredient(db.Model):
__tablename__ = "Ingredient"
Ingredient_ID = db.Column(db.Integer, primary_key=True)
Ingredient_Name = db.Column(db.String(100))
Ingredient_Calories = db.Column(db.Integer(100))
我的代碼,當我嘗試通過在bash控制檯在數據庫中創建表出現這種情況:
In [6]: from flask_app import db
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-5825668f6e50> in <module>()
----> 1 from flask_app import db
/home/Harrryj/mysite/flask_app.py in <module>()
41
42
---> 43 class Ingredient(db.Model):
44 __tablename__ = "Ingredient"
45
/home/Harrryj/mysite/flask_app.py in Ingredient()
46 Ingredient_ID = db.Column(db.Integer, primary_key=True)
47 Ingredient_Name = db.Column(db.String(100))
---> 48 Ingredient_Calories = db.Column(db.Integer(100))
49
50
TypeError: object() takes no parameters
任何幫助,將不勝感激!我知道我錯過了一些東西
工作!非常感謝 :) – Harrryj