Python之SqlAlchemy
阿新 • • 發佈:2018-06-24
元編程 false python 指定 return ima urn reat AS
1.sqlalchemy是一個orm框架,並且大量使用元編程
import sqlalchemy from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column,Integer,Date,String connect_string = "{}://{}:{}@{}:{}/{}".format( ‘mysql+pymysql‘, ‘test‘, ‘1qazxsw2‘, ‘127.0.0.1‘, ‘3306‘, ‘blog‘ ) engine = create_engine(connect_string,echo=True) Base = declarative_base() #創建基類便於實體類繼承 class Student(Base): #指定表名 __tablename__ = ‘student‘ #定義屬性對應字段 id = Column(Integer,primary_key=True,autoincrement=True) name = Column(String(64),nullable=False) age= Column(Integer) def __repr__(self): return ‘<{} id={},name={},age={}> ‘.format(self.__class__.__name__,self.id,self.name,self.age) #創建繼承自base的所有表 Base.metadata.create_all(engine)
Python之SqlAlchemy