在SQLAlchemy中插入一行

时间:2011-10-13 11:00:41

标签: python insert sqlalchemy

围绕这个房子,似乎无法获得正确的语法。

我想要做的就是在SQLAlchemy的表中插入一行。文档对我来说没有意义:

class sqlalchemy.sql.expression.Insert(table, values=None, inline=False, bind=None, prefixes=None, returning=None, **kwargs)

我最接近的是

userChoices = meta.Session.query(model.CompUserChoices).filter(model.CompUserChoices.inmptl_user_name == postdict['userid']).filter(model.CompUserChoices.inmptl_option_id == postdict['leg']).all()

userChoices.insert({model.CompUserChoices.inmptl_user_name:postdict['userid']},\
                                        {model.CompUserChoices.inmptl_option_id:postdict['leg']},\
                                        {model.CompUserChoices.inmptl_comp_choice_id:newChoices[i]})

有人可以告诉我正确的语法!

1 个答案:

答案 0 :(得分:7)

我不确定我是否理解你要做什么,但添加一行通常并不比创建模型对象并将其添加到会话更难。

userChoices = model.CompUserChoices()
# Do stuff on userChoices
meta.Session.add(userChoices)
meta.Session.commit()  # if you have to commit manually