# session.add(dep_fn) wh = Employee( emp_name='王贺', gender='male', birth_date='1993-1-1', phone='17788990022', email='*****@*****.**', dep_id=1 ) lj = Employee( emp_name='李俊', gender='male', birth_date='1995-10-1', phone='13355667788', email='*****@*****.**', dep_id=4 ) zzh = Employee( emp_name='赵子浩', gender='male', birth_date='1999-5-4', phone='13556789987', email='*****@*****.**', dep_id=4 ) # session.add_all([wh, lj, zzh]) wh20181001 = Salary(emp_id=1, date='2018-10-01', basic=15000, awards=5000) session.add(wh20181001) session.commit() session.close()
#!/usr/bin/env python3 # -*- coding:utf-8 -*- from dbconn import Departments, Session, Employees, Salary session = Session() #创建session会话 dev = Departments(dep_name='development') print(dev.dep_name) print(dev.dep_id) dev = Departments(dep_name="人事部", dep_id=2) session.add(dev) #添加一个 ops = Departments(dep_name="运维部") finance = Departments(dep_name="财务部") session.add_all([ops, finance]) #添加多个 session.commit() #print(dev.dep_id) session.close()
dep_dev = Department(dep_name='development') dep_hr = Department(dep_name='人事部') dep_op = Department(dep_name='运维部') # session.add_all([dep_hr,dep_op]) #session.add(dep_dev) my = Employee(emp_name='马云', gender='malse', birth_date='1991-01-02', phone='15988663322', email='*****@*****.**', dep_id=3) mht = Employee(emp_name='马化腾', gender='malse', birth_date='1991-05-02', phone='15988776655', email='*****@*****.**', dep_id=3) lyh = Employee(emp_name='李彦宏', gender='malse', birth_date='1991-06-02', phone='15999886655', email='*****@*****.**', dep_id=3) my201810 = Salary(emp_id=1, date='2018-10-01', basic=15000, awards=5000) session.add(my201810) #session.add_all([my,mht,lyh]) session.commit() session.close()
#!/usr/bin/env python3 # _*_coding:utf8_*_ from dbconn import Departments, Session hr = Departments(dep_id=1, dep_name='hr') ops = Departments(dep_id=2, dep_name='operations') dev = Departments(dep_id=3, dep_name='development') finance = Departments(dep_id=4, dep_name='财务部') deps = [ops, dev] print(hr.dep_id, hr.dep_name) session = Session() session.add(hr) session.add_all(deps) session.add(finance) session.commit() session.close()
from dbconn import Session, Salary aug2018 = Salary(date='2018-8-10', emp_id=1, basic=10000, awards=2000) session = Session() session.add(aug2018) session.commit() session.close()
email='*****@*****.**', dep_id=2) ds = Employees(emp_id=5, name='德斯', gender='男', phone='18923745193', email='*****@*****.**', dep_id=2) nd = Employees(emp_id=6, name='牛顿', gender='女', phone='10438201348', email='*****@*****.**', dep_id=4) te = Employees(emp_id=7, name='test', gender='fale', phone='12345678910', email='*****@*****.**', dep_id=1) emps1 = [kellon, moly] emps2 = [kn, ds, nd] session = Session() session.add(te) session.add(xl) session.add_all(emps1) session.add_all(emps2) session.commit() session.close()
#SQLAlchemy #向表中添加内容 from dbconn import Session, Departments, Employees, Salary hr = Departments(dep_name='hr') ##向表Departments添加hr print(hr.dep_id) # 此时还没有在数据库中创建记录,所以是None print(hr.dep_name) session = Session() # 建立到数据库的会话连接 session.add(hr) # 真正向数据库写入记录 # op=Departments(dep_id=2,dep_name='运维部') # session=Session() # session.add(op) # dev=Departments(dep_id=3,dep_name='开发部') # qa=Departments(dep_id=4,dep_name='测试部') # session=Session() # session.add_all([dev,qa]) ##向employees表添加内容 bob = Employees(emp_id=1, emp_name='Bob', gender='male', birth_date='1995-08-23', email='*****@*****.**', dep_id='1') john = Employees(emp_id=2, emp_name='John', gender='male', birth_date='1992-06-2',
from dbconn import Department, Student, Session session = Session() dep_hr = Department(dep_name='人事部') dep_op = Department(dep_name='运维部') dep_fn = Department(dep_id=3, dep_name='财务部') session.add(dep_fn) # session.add_all([dep_hr,dep_op]) session.commit() session.close()
from dbconn import Departments, Session session = Session() hr = Departments(dep_id=1, dep_name='人事部') ops = Departments(dep_id=2, dep_name='运维部') dev = Departments(dep_id=3, dep_name='开发部') devops = Departments(dep_id=4, dep_name='DEVOPS') finance = Departments(dep_id=5, dep_name='财务部') deps = [ops, dev, devops, finance] session.add(hr) session.add_all(deps) session.commit() session.close()