Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python sqlalchemy query()函数实际上在做什么?_Python_Sqlalchemy - Fatal编程技术网

Python sqlalchemy query()函数实际上在做什么?

Python sqlalchemy query()函数实际上在做什么?,python,sqlalchemy,Python,Sqlalchemy,我正在阅读SQLAlchemy,对query()函数有点困惑 from sqlalchemy import create_engine engine = create_engine('sqlite:///users.db', echo=True) from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() from sqlalchemy import Column, Integer, Str

我正在阅读SQLAlchemy,对query()函数有点困惑

from sqlalchemy import create_engine
engine = create_engine('sqlite:///users.db', echo=True)
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

from sqlalchemy import Column, Integer, String
from sqlalchemy import Sequence
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
    name = Column(String(50))
    fullname = Column(String(50))
    password = Column(String(12))
    def __init__(self, name, fullname, password):
        self.name = name
        self.fullname = fullname
        self.password = password
    def __repr__(self):
       return "<User('%s','%s', '%s')>" % (self.name, self.fullname, self.password)
Base.metadata.create_all(engine)
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()enter code here
ed_user = User('ed', 'Ed Jones', 'edspassword')
session.add(ed_user)
our_user = session.query(User).filter_by(name='ed').first()
session.add_all([User('wendy', 'Wendy Williams', 'foobar'),User('mary', 'Mary Contrary', 'xxg527'),User('fred', 'Fred Flinstone', 'blah')])
session.commit()
它实际上发出了

BEGIN (implicit)
INSERT INTO users (name, fullname, password) VALUES (?, ?, ?)
('ed', 'Ed Jones', 'edspassword')
SELECT users.id AS users_id,
        users.name AS users_name,
        users.fullname AS users_fullname,
        users.password AS users_password
FROM users
WHERE users.name = ?
 LIMIT ? OFFSET ?
('ed', 1, 0)
但是,直到执行session.commit()之后,数据库才得到更新(它是空的)

我的问题是: 1.为什么挂起的数据被刷新,甚至显示INSERT INTO users,数据库仍然是空的? 2.数据库不包含刷新的数据,但如果通过运行ed_user.id返回正确的主键


谢谢

您正在使用事务。数据库事务隔离不同的连接,并确保数据库处于一致状态。只有在您提交事务后,其他连接才能看到您的更改

您的SQLAlchemy连接可以看到它所做的所有更改,因为它是自己事务的一方

请注意,另一个选项是中止事务,并回滚所有更改,并且您的新对象将不再存在于数据库中,无论是在此连接中还是任何其他连接中

是一个普遍的概念,而不是特定于炼金术

BEGIN (implicit)
INSERT INTO users (name, fullname, password) VALUES (?, ?, ?)
('ed', 'Ed Jones', 'edspassword')
SELECT users.id AS users_id,
        users.name AS users_name,
        users.fullname AS users_fullname,
        users.password AS users_password
FROM users
WHERE users.name = ?
 LIMIT ? OFFSET ?
('ed', 1, 0)