python sqlalchemy 中文乱码怎么办
1个回答
展开全部
from sqlalchemy import Column, String, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
# 创建对象的基类:
Base = declarative_base()
# 定义User对象:
class USer(Base):
__tablename__='user2'
name = Column(String(20))
engine = create_engine('mysql+mysqldb://root:123@localhost:3306/test?charset=utf8')
#解决了存储到数据库中文乱码的问题
DBSession = sessionmaker(bind=engine)
session = DBSession()
new_user = USer(name='Bob')
# 添加到session:
session.add(new_user)
# 提交即保存到数据库:
session.commit()
# 关闭session:
session.close()
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
# 创建对象的基类:
Base = declarative_base()
# 定义User对象:
class USer(Base):
__tablename__='user2'
name = Column(String(20))
engine = create_engine('mysql+mysqldb://root:123@localhost:3306/test?charset=utf8')
#解决了存储到数据库中文乱码的问题
DBSession = sessionmaker(bind=engine)
session = DBSession()
new_user = USer(name='Bob')
# 添加到session:
session.add(new_user)
# 提交即保存到数据库:
session.commit()
# 关闭session:
session.close()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询