嘿,我正在制作一个社交媒体应用程序,想保存一个帖子到我的数据库。但我一直都有这种感觉

2024-04-27 02:49:23 发布

您现在位置:Python中文网/ 问答频道 /正文

你知道吗sqlalchemy.exc.OperationalError错误 sqlalchemy.exc.OperationalError错误:(sqlite3.OperationalError)没有这样的表:blogpost[SQL:'插入blogpost(标题、作者、发布日期、内容)值(?)?, ?, ?, ?)'][参数:('qedqdq','qwdqdwd','2018-12-31 22:51:35.669388','qwdqwdw')](此错误的背景信息位于:http://sqlalche.me/e/e3q8

当我把文章发到我的数据库后,我得到了错误

下面是我正在使用的一些代码。你知道吗

任何帮助都会很好谢谢!!!你知道吗

你知道吗应用程序类型你知道吗

import os  
from flask import Flask, render_template, request, redirect, url_for  
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

app = Flask(__name__)  

app.config['SQLALCHEMY_DATABASE_URL'] = 'sqlite:///Users/------/Desktop/face0/face/blog.db'

db = SQLAlchemy(app)

class Blogpost(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(50))
    author = db.Column(db.String(20))
    date_posted = db.Column(db.DateTime)
    content = db.Column(db.Text)

@app.route('/addpost', methods=['POST'])
def addpost():
    title = request.form['title']
    author = request.form['author']
    content = request.form['content']

    post = Blogpost(title=title, author=author, content=content, date_posted=datetime.now())

    db.session.add(post)
    db.session.commit()

    return redirect(url_for('index'))

Tags: fromimportformappdbdatetimesqlalchemytitle
1条回答
网友
1楼 · 发布于 2024-04-27 02:49:23

flask-sqlalchemy documentation中所述,首先需要通过调用create_all函数来创建数据库。你知道吗

To create the initial database, just import the db object from an interactive Python shell and run the SQLAlchemy.create_all() method to create the tables and database:

>>> from yourapplication import db
>>> db.create_all()

在您的情况下,可能会出现以下情况:

$ python
>>> from app import db
>>> db.create_all()
>>> exit()

相关问题 更多 >