You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
802 B
35 lines
802 B
import sqlalchemy |
|
from sqlalchemy.orm import declarative_base, sessionmaker, Session # declarative 声明式 |
|
from sqlalchemy import create_engine, Column, Integer, String |
|
|
|
# 创建数据引擎 |
|
HOST = "home.rogersun.cn" |
|
USER = "root" |
|
PWD = "Sxzgx1209" |
|
PORT = "3306" |
|
DB = "orm_sqlalchemy" |
|
|
|
URI = f"mysql+pymysql://{USER}:{PWD}@{HOST}:{PORT}/{DB}" |
|
engine = create_engine(URI, echo=True) |
|
|
|
# 创建session |
|
session = sessionmaker(engine) |
|
|
|
# with session |
|
# with Session(engine) as db_session: |
|
# session.begin() |
|
# try: |
|
# db_session.add("some_object") |
|
# db_session.add("some_other_object") |
|
# except: |
|
# db_session.rollback() |
|
# raise |
|
# else: |
|
# db_session.commit() |
|
|
|
# 生成orm基类 |
|
Base = declarative_base() |
|
|
|
|
|
class User(Base): |
|
__tablename__ = 'user'
|
|
|