python3 + flask + sqlalchemy +orm(2):數據庫中添加表

往數據庫中添加一張保存文章的表,代表爲article,字段有id,title,contentmysql

一樣一個配置文件:config.pysql

DEBUG = True
#dialect+driver://root:1q2w3e4r5t@127.0.0.1:3306/
DIALECT = 'mysql'
DRIVER='pymysql'
USERNAME = 'root'
PASSWORD = '1q2w3e4r5t'
HOST = '127.0.0.1'
PORT = 3306
DATABASE = 'db_demo1'

SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT,DRIVER,USERNAME,PASSWORD,HOST,PORT,DATABASE)
SQLALCHEMY_TRACK_MODIFICATIONS = False
print(SQLALCHEMY_DATABASE_URI)

flask app 中新建一個class Blog,裏面定義好id ,title ,content。代碼執行到db.create_all()時,會自動在數據庫中建立一個表,代表爲blog數據庫

from flask import Flask
import config

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.declarative import declarative_base

app = Flask(__name__)
app.config.from_object(config)

db = SQLAlchemy(app)
Base = declarative_base()


class Blog(db.Model):
    __tablename__ = 'blog'
    id  = db.Column(db.Integer,primary_key=True,autoincrement=True)
    title = db.Column(db.String(100),nullable=False)
    content = db.Column(db.Text,nullable=True)


db.create_all()


@app.route('/')
def index():
    return 'index'


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

啓動flask app,數據庫中查詢表和表結構以下,有新增相應的表,說明新建表成功flask

 

數據庫表中的數據增刪改查session

    #新增
    blog = Blog(title="first blog",content="this is my first blog")
    db.session.add(blog)
    db.session.commit()

    #查詢
    #res =Blog.query.filter(Blog.title=="first blog")[0]

    res =Blog.query.filter(Blog.title=="first blog").first()
    print(res.title)
     #修改
    blog_edit = Blog.query.filter(Blog.title=="first blog").first()
    blog_edit.title = "new first blog"
    db.session.commit()
    #刪除
    blog_delete  = Blog.query.filter(Blog.title=="first blog").first()
    db.session.delete(blog_delete)
    db.session.commit()

完整代碼app

from flask import Flask
import config

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.declarative import declarative_base

app = Flask(__name__)
app.config.from_object(config)

db = SQLAlchemy(app)
Base = declarative_base()


class Blog(db.Model):
    __tablename__ = 'blog'
    id  = db.Column(db.Integer,primary_key=True,autoincrement=True)
    title = db.Column(db.String(100),nullable=False)
    content = db.Column(db.Text,nullable=True)


db.create_all()


@app.route('/')
def index():
    #新增
    blog = Blog(title="first blog",content="this is my first blog")
    db.session.add(blog)
    db.session.commit()

    #查詢
    #res =Blog.query.filter(Blog.title=="first blog")[0]

    res =Blog.query.filter(Blog.title=="first blog").first()
    print(res.title)
     #修改
    blog_edit = Blog.query.filter(Blog.title=="first blog").first()
    blog_edit.title = "new first blog"
    db.session.commit()
    #刪除
    blog_delete  = Blog.query.filter(Blog.title=="first blog").first()
    db.session.delete(blog_delete)
    db.session.commit()


    return 'index'


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

 

 

歡迎掃描二維碼關注技術公衆號:爲測,上面有自動化測試方面技術文章測試

 

相關文章
相關標籤/搜索