SQLAlchemy flush()和commit()SQLAlchemy 之间有什么区别?
我已经阅读了文档,但没有更明智 - 他们似乎假设我没有预先理解.
我对它们对内存使用的影响特别感兴趣.我正在从一系列文件(总共约500万行)中将一些数据加载到数据库中,并且我的会话偶尔会崩溃 - 它是一个大型数据库和一台内存不足的机器.
我想知道我是否使用了太多但commit()没有足够的flush()电话 - 但是如果没有真正了解其中的差异,那就很难说了!
假设我有一个表tags,其中有一个字段count,表示items已使用给定标签标记了多少.
在使用现有标记添加新项目后,如何在SQLAlchemy中增加此计数器?
使用纯SQL我会执行以下操作:
INSERT INTO `items` VALUES (...)
UPDATE `tags` SET count=count+1 WHERE tag_id=5
Run Code Online (Sandbox Code Playgroud)
但是我如何count=count+1在SQLAlchemy中表达?
谢谢,Boda Cydo.
我正在学习SQLAlchemy而且我被困住了.我有一个SQL表(table1)有两个字段:'name'和'other_names'
我有一个包含两列的excel文件:
first_name alias
paul patrick
john joe
simon simone
john joey
john jo
Run Code Online (Sandbox Code Playgroud)
我想将excel文件读入我的table1,所以它看起来像这样(即同一行的所有别名都在一行):
paul patrick
john joe,joey,jo
simon simone
Run Code Online (Sandbox Code Playgroud)
这是我试图做的想法.我试过的代码(带注释):
for line in open('file.txt', 'r'): #for each line in the excel file
line = line.strip().split('\t') #split each line with a name and alias
first_name = line[0] #first name is the name before the tab
alias = line[1] #alias is the name after the tab
instance =
Session.query(session,tbs['table1'].name).filter_by(name=first_name) #look through the database table, by name field, and see if …Run Code Online (Sandbox Code Playgroud) PostgreSQL和SQL定义了Serializable事务隔离级别.如果将事务隔离到此级别,则冲突的并发事务将中止并需要重试.
我熟悉Plone/Zope世界的事务重试概念,在发生事务冲突的情况下可以重放整个HTTP请求.如何使用SQLAlchemy(以及可能使用zope.sqlalchemy)实现类似的功能?我试着阅读zope.sqlalchemy和Zope事务管理器的文档,但这对我来说并不明显.
特别想要这样的东西:
# Try to do the stuff, if it fails because of transaction conflict do again until retry count is exceeded
with transaction.manager(retries=3):
do_stuff()
# If we couldn't get the transaction through even after 3 attempts, fail with a horrible exception
Run Code Online (Sandbox Code Playgroud) 我有一个模型:
class Announcements(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(150), nullable=False)
text = db.Column(db.Text(), nullable=False)
category = db.Column(db.Integer(), nullable=False)
subcategory = db.Column(db.Integer(), nullable=False)
offer_type = db.Column(db.Integer(), nullable=False)
url = db.Column(db.String(150), nullable=True)
status = db.Column(db.Integer(), nullable=False)
def __repr__(self):
return '<Announcement %r>' % self.name
Run Code Online (Sandbox Code Playgroud)
和公共功能:
def update(id):
// UPDATE HERE
Run Code Online (Sandbox Code Playgroud)
如何使用 Flask-SQLAlchemy通过传入id的函数更新注释update()?
我在文档中没有看到任何更新行的示例