修改数据库中的数据


from flask import Flask
from exts import db
from models import Article

app = Flask(__name__)
app.config.from_pyfile('config.py')
db.init_app(app)

@app.route('/')
def index():
    # 使用query查询到数据后进行修改
    article1 = Article.query.first()
    article1.title = '文章标题11'
    article1.content = '文章内容11'
    db.session.commit()

    # 使用filter查询到数据后进行修改
    article2 = Article.query.filter(Article.title == '文章标题1').first()
    article2.title = '文章标题11'
    article2.content = '文章内容11'
    db.session.commit()

    # 使用filter_by查询到数据后进行修改
    article3 = Article.query.filter_by(title='文章标题1').first()
    article3.title = '文章标题11'
    article3.content = '文章内容11'
    db.session.commit()

    # 使用get查询到数据后进行修改
    article4 = Article.query.get(8)
    article4.title = '文章标题11'
    article4.content = '文章内容11'
    db.session.commit()

    return 'Hello World!'

if __name__ == '__main__':
    app.run()

results matching ""

    No results matching ""