Python: SQL Alchemy
sqlalchemy learning notes
so, it's a object relational shit.
but, basically, you just create a class/object in python, and each such corresponds to a table table. the in the class, the properties are just tablle columnns. Then, each instance of the class is different rows.
some like that.
http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html
show version
import sqlalchemy sqlalchemy.__version__ # 0.9.0
To connect we use create_engine()
:
from sqlalchemy import create_engine engine = create_engine('sqlite:///:memory:', echo=True)
The return value of create_engine()
is an instance of Engine
, and it represents the core interface to the database,
The first time a method like Engine.execute() or Engine.connect() is called, the Engine establishes a real DBAPI connection to the database, which is then used to emit the SQL. When using the ORM, we typically don't use the Engine directly once created; instead, it's used behind the scenes by the ORM as we’ll see shortly.
declare mapping
declare base, then create a table schema
from sqlalchemy.ext.declarative import declarative_base Base = declarative_base()
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)