麻烦各位大神帮忙翻译一下谢谢。
[root@room9pc01day11]#catdbconn.py1"MariaDB[(none)]>CREATEDATABASEtarenaDEFAULTCHARSE...
[root@room9pc01 day11]# cat dbconn.py
1 "MariaDB [(none)]> CREATE DATABASE tarena DEFAULT CHARSET='utf8';"
2
3 from sqlalchemy import create_engine
4 from sqlalchemy.ext.declarative import declarative_base
5 from sqlalchemy import Column, Integer, String
6 from sqlalchemy.orm import sessionmaker
7
8 engine = create_engine(
9 'mysql+pymysql://root:tedu.cn@localhost/tarena',
10 encoding='utf8', echo=True
11 )
12 # mysql+pymysql://用户名:密码@主机/库名
13 Base = declarative_base() # 创建ORM所需要的基类
14 Session = sessionmaker(bind=engine)
[root@room9pc01 day11]# cat insert_tarena1.py
from dbconn import Session, Departments
hr = Departments(dep_name='hr')
print(hr.dep_id) # 此时还没有在数据库中创建记录,所以是None
print(hr.dep_name)
session = Session() # 建立到数据库的会话连接
session.add(hr) # 真正向数据库写入记录
session.commit()
print(hr.dep_id)
session.close() 展开
1 "MariaDB [(none)]> CREATE DATABASE tarena DEFAULT CHARSET='utf8';"
2
3 from sqlalchemy import create_engine
4 from sqlalchemy.ext.declarative import declarative_base
5 from sqlalchemy import Column, Integer, String
6 from sqlalchemy.orm import sessionmaker
7
8 engine = create_engine(
9 'mysql+pymysql://root:tedu.cn@localhost/tarena',
10 encoding='utf8', echo=True
11 )
12 # mysql+pymysql://用户名:密码@主机/库名
13 Base = declarative_base() # 创建ORM所需要的基类
14 Session = sessionmaker(bind=engine)
[root@room9pc01 day11]# cat insert_tarena1.py
from dbconn import Session, Departments
hr = Departments(dep_name='hr')
print(hr.dep_id) # 此时还没有在数据库中创建记录,所以是None
print(hr.dep_name)
session = Session() # 建立到数据库的会话连接
session.add(hr) # 真正向数据库写入记录
session.commit()
print(hr.dep_id)
session.close() 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询