赞
踩
- from faker import Faker
- from dataclasses import dataclass, field, asdict
- from sqlalchemy import create_engine, Table, MetaData
- from sqlalchemy.orm import sessionmaker
-
- # dataclasses库需要Python3.7及以上版本才有
-
- fake_zh_cn = Faker(locale='zh_CN')
-
-
- @dataclass
- class Emp:
- CenterName: str = field(default_factory=fake_zh_cn.name)
- Description: str = field(default_factory=fake_zh_cn.phone_number)
- Deleted: int = field(default=1)
- CreatedBy: int = field(default=1)
- CreatedTime: str = field(default_factory=fake.date)
- UpdatedBy: int = field(default=1)
- UpdatedTime: str = field(default_factory=fake.date)
-
-
- metadata = MetaData()
- engine = create_engine("mssql+pymssql://sa:sa@10.10.10.10/test")
- DBSession = sessionmaker(bind=engine)
- session = DBSession()
-
- ex_table = Table('dbo.Center', metadata, autoload=True, autoload_with=engine)
- for _ in range(10):
- session.execute(ex_table.insert(), asdict(Emp()))
- session.commit()
- session.close()
-
- result = engine.execute('''select * from dbo.Center''')
-
- for row in result:
- print(row)
后续再优化吧!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。