1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
from sqlalchemy import create_engine from sqlalchemy import Table, MetaData, Column, Integer, String from sqlalchemy.orm import mapper, sessionmaker
engine = create_engine("mysql+pymysql://root:123456@localhost/test", encoding='utf-8', echo=True) metadata = MetaData() user = Table('user', metadata, Column('id', Integer, primary_key=True,autoincrement=True), Column('name', String(50)), Column('password', String(12)) ) class User(object): def __init__(self, name, password): self.name = name self.password = password
mapper(User, user)
Session_class = sessionmaker(bind=engine) Session = Session_class()
user_obj = User(name="fgf",password="123456") print(user_obj.name,user_obj.id) Session.add(user_obj)
print(user_obj.name,user_obj.id) Session.commit()
|