from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
def connect_to_database_orm():
"""
This module is to connect to database 'ANNEDBSE'
:return: session
"""
# create an engine to point to my database 'ANNEDBSE'
engine = create_engine('mysql+pymysql://root:123456@192.152.156.134:3306/ANNEDBSE')
# define a Session class by bind of sessionmaker
Session = sessionmaker(bind=engine)
# create a session instance by the above new Session class
session = Session()
# return session and engine
return session, engine
上面create_engine里的字符串mysql+pymysql://root:123456@192.152.156.134:3306/ANNEDBSE
1. 数据库类型:mysql
2.数据库驱动:pymysql
3.用户认证:root:123456 ,这个是连接数据库mysql的用户名密码,我是在博客https://blog.csdn.net/LelemamaAnne/article/details/112479185 的第四步运行容器时配置的。
4.数据库位置:数据库服务器所在的主机IP和port:192.152.156.134:3306
5.数据库名:ANNEDBSE