Article From:https://www.cnblogs.com/wuheng-123/p/9688625.html
from sqlalchemy import create_engine,Column,Integer,String
from sqlalchemy.ext.declarative import declarative_base

HOSTNAME = '127.0.0.1'

PORT = 3306

DATABASE = 'first_sqlalchemy'

USERNAME = 'root'

PASSWORD = '123456'

#dialect+driver://username:password@host:port/database
DB_URI = "mysql+pymysql://{username}:{password}@{host}:{port}/" \
         "{db}?charset=utf8".format(username=USERNAME,password=PASSWORD,host=HOSTNAME,port=PORT,db=DATABASE)


engine = create_engine(DB_URI)

Base = declarative_base(engine)

# create table person(id int primary key autoincrement, name varchar(32) unique,age smallint unsigende)

#1.Create a ORM model. This ORM model must inherit Sqlalchemy to provide our base class.


class Person(Base):
    __tablename__ = 'person'

#2.Create some properties in the ORM model to map the fields in the table one by one. These properties must be
#sqlalchemyProvide us with good data types.
    id = Column(Integer,primary_key=True,autoincrement=True)
    name = Column(String(32))
    age = Column(Integer)

#3.The created ORM model is mapped to the data.
Base.metadata.create_all()

 

Link of this Article: Sqlalchemy mapping database

Leave a Reply

Your email address will not be published. Required fields are marked *