【问题标题】:How to find all columns with a particular number as an attribute? [duplicate]如何查找具有特定数字作为属性的所有列? [复制]
【发布时间】:2015-01-18 07:45:19
【问题描述】:

我在 SQLAlchemy 中有以下表类:

class pinCodes(db.Model):
id = db.Column(db.Integer, primary_key=True)
officename = db.Column(db.String(60), nullable=False)
pincode = db.Column(db.Integer, nullable=False)
taluk = db.Column(db.String(50), nullable=False)
district = db.Column(db.String(50), nullable=False)
state = db.Column(db.String(50), nullable=False)
__tablename__ = "pincodes"

def __init__(self, officename, pincode, taluk, district, state):
    self.officename = officename
    self.pincode = pincode
    self.taluk = taluk
    self.district = district
    self.state = state 

def __repr__(self):
    return '<officename {}>'.format(self.officename)

现在我想返回所有带有“pincode”的列作为特定值: 代码如下:

def fmt_pin(code):
    return code.replace(" ","").replace("-","").replace("_","").strip()

def int_pin(code):
    code = fmt_pin(code)
    return int(code)



@app.route('/find_pin_codes', methods=['POST','GET'])
def pincodes():
    if request.method == 'POST':  

        if request.form.get("pincode", "") != "":
            pincode = request.form['pincode']
            try:
                num = pinCodes.query.filter(pincode = int_pin(pincode)).all()
                return render_template('pincodes.html', 
                                        bypincode = num

                                        )
            except Exception as e:
                return render_template('pincodes.html',
                                        error = e)

但是当我搜索一个数字时,我收到以下错误: filter() 得到了一个意外的关键字参数“pincode”。

编辑:使用 filter_by 后,我没有得到任何结果。 这是我的 Jinja 模板代码:

  {% if pincode is defined %}

    <thead>
        <tr>
            <td><h4>Pincode</h4></td>
            <td><h4>Office Name</h4></td>
            <td><h4>Taluk</h4></td>
            <td><h4>District</h4></td>
            <td><h4>State</h4></td>
        </tr>
    </thead>

    <tbody>

    {% for o in bypincode %}
        <tr>
            <td><h5>{{ o.pincode }}</h5></td>
            <td><h5>{{ o.officename }}</h5></td>
            <td><h5>{{ o.taluk }}</h5></td>
            <td><h5>{{ o.district }}</h5></td>
             <td><h5>{{ o.state }}</h5></td>
        </tr>
    {% endfor %}  
    {% endif %}

【问题讨论】:

  • 顺便说一句,你不需要!= '',因为空字符串在Python中是假的。
  • 在我删除 {% if pincode is defined %} 后它现在可以工作了

标签: python python-2.7 flask flask-sqlalchemy


【解决方案1】:

尝试使用filter_by 而不是filter

filter_by(**kwargs) 工作正常:

pinCodes.query.filter_by(pincode = int_pin(pincode))

但是filter(*criterion) 没有关键字参数,你应该像这样使用它:

pinCodes.query.filter(pinCodes.pincode == int_pin(pincode))

【讨论】:

  • 顺便说一句,您可能想在密码上创建一些索引。否则,每次过滤时数据库都会扫描整个表。
  • 感谢您澄清 fliter 和 fliter_by。顺便说一句,在密码上创建索引是什么意思。我不知道那是什么意思。谢谢!
  • pincode = db.Column(db.Integer, index=True, nullable=False) 可以在密码上创建索引。
  • 使用索引,搜索列可能会更快。但是插入或更新可能会更慢。您可以搜索何时使用索引获取详细信息。例如,stackoverflow.com/questions/79241/…
猜你喜欢
  • 2013-01-04
  • 2010-10-17
  • 1970-01-01
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
  • 2011-04-16
  • 2014-07-28
相关资源
最近更新 更多