Introduction to Databases in Python
Jason Myers
Co-Author of Essential SQLAlchemy and Software Engineer
create_engine()
statement will create the database and file is they do not already existfrom sqlalchemy import (Table, Column, String, Integer, Decimal, Boolean)
employees = Table('employees', metadata, Column('id', Integer()), Column('name', String(255)), Column('salary', Decimal()), Column('active', Boolean()))
metadata.create_all(engine)
engine.table_names()
[u'employees']
autoload
keyword arguments with Column objectscreate_all()
method on the MetaData instanceunique
forces all values for the data in a column to be uniquenullable
determines if a column can be empty in a rowdefault
sets a default value if one isn't supplied.employees = Table('employees', metadata, Column('id', Integer()), Column('name', String(255), unique=True, nullable=False), Column('salary', Float(), default=100.00), Column('active', Boolean(), default=True))
employees.constraints
{CheckConstraint(...
Column('name', String(length=255), table=<employees>, nullable=False),
Column('salary', Float(), table=<employees>,
default=ColumnDefault(100.0)),
Column('active', Boolean(), table=<employees>,
default=ColumnDefault(True)), ...
UniqueConstraint(Column('name', String(length=255),
table=<employees>, nullable=False))}
Introduction to Databases in Python