Yi骑绝尘 2016-11-27 02:14 采纳率: 0%
浏览 1294

SQLAlchemy 访问Mysql数据库乱码问题,如何解决?

1.写了一段使用SQLAlchemy 编程的代码,程序本身没问题,就是访问数据库时产生一个错误提示,但是即使有错误代码页能正常运行,操作结果也正确。这个错误是关于代码编码方式的错误,但是调了很长时间这个错误就是不能消除,求解救2.这里是代码:(1)orm.py

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column,Integer,String
Base = declarative_base()
class Account(Base):
tablename = u'account'
id = Column(Integer,primary_key = True)
salary = Column(Integer)
username = Column(String(20),nullable = False)
password = Column(String(200),nullable = False)
title = Column(String(50))

def is_active(self):
    return True
def get_id(self):
    return self.id
def is_authenticated(self):
    return True
def is_anonymous(self):
    return False

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session,sessionmaker
db_connect_string = 'mysql://root:admin@localhost:3306/test_database?charset=utf8'
ssl_args = {'ssl':{'cert':'/home//ssl/client-cert.pem',
'key':"/home/shouse/ssl/client-cert.pem",
'ca':'/home/shouse/ssl/ca-cert.pem'}}
engine = create_engine(db_connect_string,connect_args = ssl_args)
SessionType = scoped_session(sessionmaker(bind = engine,expire_on_commit = False))
def GetSession():
return SessionType()
from contextlib import contextmanager
@contextmanager
def session_scope():
session = GetSession()
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()
(2)databaseoperation.py代码:

-*- coding:utf-8 -*-

import orm
from sqlalchemy import or_
def InsertAccount(user,password,title,salary):
with orm.session_scope() as session:
account = orm.Account(username = user,password =password,title = title,salary = salary)
session.add(account)
def GetAccount(id=None,username = None):
with orm.session_scope() as session:
return session.query(orm.Account).filter(or_(orm.Account.id==id,orm.Account.username==username)).first()
def Delete(username):
with orm.session_scope() as session:
account = GetAccount(username = username)

if account:
session.delete(account)
def Update(id,username,password,title,salary):
with orm.session_scope() as session:
account = session.query(orm.Account).filter(orm.Account.id==id).first()
if not account:return
account.username=username
account.password=password
account.title = title
account.salary = salary

InsertAccount('Lliy','123','System Manager',3000)
InsertAccount('Rebeca Li','12221','Accountant',3000)

GetAccount(2)

Delete('David Li')
Update(1,'admin','1111','System Admin',2000)
3.错误提示:

4.数据库截图

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2017-02-21 18:41
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 安装华大九天aether
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证