python - 请问用sqlalchemy如何根据中文字段名做查询?

查看:538
本文介绍了python - 请问用sqlalchemy如何根据中文字段名做查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

数据库有个中文字段名叫'名称',如何根据这个中文字段做搜索 。

result=Minicomputer.query.filter_by(u'名称'='CC670a').first()

报错如下

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    app = create_app(os.getenv('FLASK_CONFIG') or 'default')
  File "/mnt/hgfs/python/flask/Project/__init__.py", line 34, in create_app
    from .main import main as main_blueprint
  File "/mnt/hgfs/python/flask/Project/main/__init__.py", line 3, in <module>
    from . import views, errors
  File "/mnt/hgfs/python/flask/Project/main/views.py", line 95
    result=Minicomputer.query.filter_by(u'名称'='CC670a').first()
SyntaxError: keyword can't be an expression

解决方案

做一个表名的映射即可

class Minicomputer(Base):
    __table__ = Base.metadata.tables['minicomputer']
    name = __table__.c[u'名称']


    def __repr__(self):

        return '<Minicomputer %r>' % self.name

这篇关于python - 请问用sqlalchemy如何根据中文字段名做查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆