简体   繁体   English

MySql查询的烧瓶HTML表单字段在macOS Sierra上返回错误

[英]Flask HTML form field for MySql query returning error on macOS Sierra

Hello Stackoverflow community 你好Stackoverflow社区

I have been running in some strange problems ever since I up-graded to macOS Sierra and MariaDB 10.2.9 and the code (exactly the same that I was using before on Windows) now throws a "TypeError: 'NoneType' object is not iterable". 自从升级到macOS Sierra和MariaDB 10.2.9以来,我一直在遇到一些奇怪的问题,并且代码(与Windows之前使用的代码完全相同)现在抛出“ TypeError:'NoneType'对象不可迭代”。 I cannot figure out why, but if you have any idea I would greatly appreciate. 我不知道为什么,但是如果您有任何想法,我将不胜感激。

The code is the same as in my question from last year under: FLASK HTML field for Mysql query 代码与去年的问题相同: Mysql查询的FLASK HTML字段

All I am doing is making a request from an HTML form method to a Mysql database using PyMysql, Flask and MariaDB(Mysql) 我正在做的就是使用PyMysql,Flask和MariaDB(Mysql)从HTML表单方法向Mysql数据库发出请求

Here the Form Field and the table to be displayed (this is working with other queries): 此处的“表单字段”和要显示的表(正在与其他查询一起使用):

 <form method="GET" action>
    <div class="form-group">
        <div class="col-sm-3">
            <input type="text" placeholder="City Name" name="City_Name" class="form-control">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-2">
            <input type="submit" value="SEARCH" class="btn btn-primary btn-block">
        </div>
    </div>
</form>

<table class="table table-striped">


        <tr>
          <th>PO_Number</th>
          <th>Plant Name</th>
          <th>Material</th>
        </tr>


       {% for row in tabledata %}
       <tr>

         <td>{{ row['PO_Number'] }}</td>
         <td>{{ row['PlantName'] }}</td>
         <td>{{ row['MaterialName'] }}</td>
       </tr>
       {% endfor %}

Here the Flask route: 这是Flask路线:

from flask import Flask, render_template, request, url_for
from dbhelper_single_search import DBHelper


app = Flask(__name__)
DB = DBHelper()

@app.route('/table')
def table():
    city = request.args.get('City_Name', 'New York')
    try:
        tabledata = DB.table_inputs(city)
    except Exception as e:
        print(e)
        tabledata = None
    return render_template("table.html", tabledata=tabledata)

And here the Db connection: 这是Db连接:

import pymysql

connection = pymysql.connect(host='localhost',user='root',password='',db='test',cursorclass=pymysql.cursors.DictCursor)

class DBHelper:

def table_inputs(self, cityx):
    connection = self.connect()
    PLN = "**%s**" % cityx
    try:
        query = "SELECT PersonID, LastName, FirstName, Address, City FROM persons WHERE City like '%s'" %(PLN);
        with connection.cursor(pymysql.cursors.DictCursor) as cursor:
            cursor.execute(query)
            return cursor.fetchall()
    finally:
        connection.close()

Again I would like to ask whether there is a difference that I would need to be aware of when using this on my current MacOS Maria DB set up and previous stack. 再次,我想问一下,在当前的MacOS Maria DB设置和先前的堆栈上使用此功能时是否需要了解区别。 Its just strange that the previously working code now no longer does. 以前工作的代码现在不再起作用,这很奇怪。

Thank you in advance 先感谢您

although I admit that I do not fully understand the final working code, I would like to share a working version for future reference. 尽管我承认我不完全理解最终的工作代码,但我还是希望分享一个工作版本以供将来参考。

DB Config file: 数据库配置文件:

test = False
db_username = "root"
db_password = ""

DBHelper file with query: 带有查询的DBHelper文件:

import pymysql import dbconfig 导入pymysql导入dbconfig

class DBHelper: DBHelper类:

def connect(self, database="test"):
          return pymysql.connect (host='localhost',
                user=dbconfig.db_username,
                passwd=dbconfig.db_password,
                db=database)
def table_inputs2(self, cityx):
    connection = self.connect()
    PLN = "%s" % cityx
    try:
        query = "SELECT PersonID, LastName, FirstName, Address, City FROM persons WHERE City='%s'" %(PLN);
        with connection.cursor(pymysql.cursors.DictCursor) as cursor:
            cursor.execute(query)
            return cursor.fetchall()
    finally:
        connection.cursor

App Route: 应用程序路线:

@app.route('/trtdtable2')
def trtdtable2():
    cityx = request.args.get('City_Name','New York')
    try:
        tabledata = DB.table_inputs2(cityx)
    except Exception as e:
        print(e)
        tabledata = None
    return render_template("trtdtable.html", tabledata=tabledata)

if __name__ == '__main__':
    app.run(debug=True)

HTML form as shown above. HTML表单如上所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM