Insert data into specific table (sqlalchemy)

You will probably want to look at the sqlalchemy ORM tutorial.

To get you started though, I suspect you are going to want to set up something like the following.

from sqlalchemy import Column, Integer, String
class Address(Base):
    __tablename__ = 'addresses'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    age = Column(Integer)

    def __init__(self, name, age):
        self.name = name
        self.age = age

Now since it appears you already know how to get a session, you could just do this

address = Address('Joe', 26)
session.add(address)

Although it is possible that you could probably just try to do it on the fly, it probably wouldn't make your life easier, as you'd still have to tell it what objects relate to integers, strings, etc, to map to the database table.


I solved the problem by using the sql expression language to add the new row:

engine.execute(table_addresses.insert(), name='Joe', age=20)
engine.execute(table_contacts.insert(), email='[email protected]', cellnumber='267534320')

Normaly I use the ORM but in my case it is more convinient.