当前位置:   article > 正文

Python+Faker+dataclasses +sqlalchemy 生成测试数据_sqlalchemy dataclass

sqlalchemy dataclass
  1. from faker import Faker
  2. from dataclasses import dataclass, field, asdict
  3. from sqlalchemy import create_engine, Table, MetaData
  4. from sqlalchemy.orm import sessionmaker
  5. # dataclasses库需要Python3.7及以上版本才有
  6. fake_zh_cn = Faker(locale='zh_CN')
  7. @dataclass
  8. class Emp:
  9. CenterName: str = field(default_factory=fake_zh_cn.name)
  10. Description: str = field(default_factory=fake_zh_cn.phone_number)
  11. Deleted: int = field(default=1)
  12. CreatedBy: int = field(default=1)
  13. CreatedTime: str = field(default_factory=fake.date)
  14. UpdatedBy: int = field(default=1)
  15. UpdatedTime: str = field(default_factory=fake.date)
  16. metadata = MetaData()
  17. engine = create_engine("mssql+pymssql://sa:sa@10.10.10.10/test")
  18. DBSession = sessionmaker(bind=engine)
  19. session = DBSession()
  20. ex_table = Table('dbo.Center', metadata, autoload=True, autoload_with=engine)
  21. for _ in range(10):
  22. session.execute(ex_table.insert(), asdict(Emp()))
  23. session.commit()
  24. session.close()
  25. result = engine.execute('''select * from dbo.Center''')
  26. for row in result:
  27. print(row)

后续再优化吧!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/927175
推荐阅读
相关标签
  

闽ICP备14008679号