RonnyKnoxville
RonnyKnoxville

Reputation: 6404

Inserting a row in SQLAlchemy

Been round the houses on this one, cant seem to get the right syntax.

All I want to do is insert a row into a table in SQLAlchemy. The documentation doesnt make sense to me thought:

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

The closest I have come is

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]})

Could someone please tell me the correct syntax!

Upvotes: 4

Views: 3153

Answers (1)

madjar
madjar

Reputation: 12961

I'm not sure I understand what exactly you are trying to do, but adding a row is usually no harder than creating a model object and adding it to the session.

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

Upvotes: 7

Related Questions