What is the difference between Session and db.session in SQLAlchemy?

    db_session = sessionmaker(bind=db.get_engine(db.get_app(), bind='bi_user_statistic'))
    db_session().query(xxxxx).filter()

Let's check the diferences:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://///Users/dedeco/Documents/tmp/testDb.db'
db = SQLAlchemy(app)

>>>type(db.session)
<class 'sqlalchemy.orm.scoping.scoped_session'>
or

from sqlalchemy import *
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

some_engine = create_engine('sqlite://///Users/dedeco/Documents/tmp/testDb.db')
Session = sessionmaker(bind=some_engine)
session = Session()

Base = declarative_base()

>>> type(session)
<class 'sqlalchemy.orm.session.Session'>
Basically the difference is:

In the first way you are using a API developed for the Flask framework, called Flask-SQLAlchemy. It's the option if you are creating a Flask application, because the scope of the Session can be managed automatically by your application. You have many benefits like a infrastructure to establish a single Session, associated with the request, which is correctly constructed and torn down corresponding torn down at the end of a request.
In the second way is a pure SQLAlchemy app, so if you are using a library to connect a particular database, you can use just a SQLAlchemy API, for example, for a command-line script, background daemon, GUI interface-driven application, etc.
So, in a both way you can add, like:

Using a Flask-SQLAlchemy:

class User(db.Model):
    __tablename__ = 'users'
    user_id = db.Column(db.Integer(), primary_key = True)
    user_name = db.Column(db.String(80), unique=True)
    def __init__(self, user_name):
        self.user_name = user_name

>>> db.create_all()
>>> u = User('user1')
>>> db.session.add(u)
>>> db.session.commit()
>>> users = db.session.query(User).all()
>>> for u in users:
...     print u.user_name
... 
user1
Using just SQLAlchemy:

class User(Base):
    __tablename__ = 'users'
    user_id = Column(Integer(), primary_key = True)
    user_name = Column(String(80), unique=True)

>>> u = User()
>>> u.user_name = 'user2'
>>> session.add(u)
>>> session.commit()
>>> users = session.query(User).all()
>>> for u in users:
...     print u.user_name
... 
user1
user2
Realize that I am connecting in the same database just for show that you can add using many ways.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 上一篇文章中,提到了解决赋值运算符异常安全的几种写法,其中提到了copy and swap 但并没有给出方法,这就...
    chnmagnus阅读 3,134评论 0 0
  • for 最亲爱的 shenjie 晚餐吃了好多 饱食过后总想早些睡觉 吃了好多菜式 我还是只想你 在这个暴食之后的...
    无聊很可爱阅读 1,423评论 0 0
  • 上海的冬天来的有点迟,在立冬以后下了几场雨以后,突然就降温了,楼下的小野猫再也没有碰到过了。今天本来是上吉他课...
    闲度阅读 1,891评论 2 0
  • 无论是做什么行业,无论你的战略是什么,企业重视产品总是没有错的,可能你不一定是以产品为王。 我们重视产品通常关注的...
    张振业阅读 3,573评论 0 4

友情链接更多精彩内容