THIS IS JUST FOR MY PERSONAL LEARNING- so It is not detailed instruction.
I am just keeping this for future quick reference
For this tutorial we will use an in-memory-only SQLite database. To connect we use
>>> from sqlalchemy import create_engine >>> engine = create_engine('sqlite:///:memory:', echo=True)
We create the base class using the
declarative_base() function, as follows:
>>> from sqlalchemy.ext.declarative import declarative_base >>> Base = declarative_base()
Now that we have a “base”, we can define any number of mapped classes in terms of it. We will start with just a single table called
users, which will store records for the end-users using our application. A new class called
User will be the class to which we map this table. Within the class, we define details about the table to which we’ll be mapping, primarily the table name, and names and datatypes of columns:
>>> from sqlalchemy import Column, Integer, String >>> class User(Base): ... __tablename__ = 'users' ... ... id = Column(Integer, primary_key=True) ... name = Column(String) ... fullname = Column(String) ... password = Column(String) ... ... def __repr__(self): ... return "<User(name='%s', fullname='%s', password='%s')>" % ( ... self.name, self.fullname, self.password) A class using Declarative at a minimum needs a
__tablename__attribute, and at least one
Columnwhich is part of a primary key . http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#building-a-relationship To be continued...