簡體   English   中英

MySql查詢的燒瓶HTML表單字段在macOS Sierra上返回錯誤

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

你好Stackoverflow社區

自從升級到macOS Sierra和MariaDB 10.2.9以來,我一直在遇到一些奇怪的問題,並且代碼(與Windows之前使用的代碼完全相同)現在拋出“ TypeError:'NoneType'對象不可迭代”。 我不知道為什么,但是如果您有任何想法,我將不勝感激。

代碼與去年的問題相同: Mysql查詢的FLASK HTML字段

我正在做的就是使用PyMysql,Flask和MariaDB(Mysql)從HTML表單方法向Mysql數據庫發出請求

此處的“表單字段”和要顯示的表(正在與其他查詢一起使用):

 <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 %}

這是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)

這是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()

再次,我想問一下,在當前的MacOS Maria DB設置和先前的堆棧上使用此功能時是否需要了解區別。 以前工作的代碼現在不再起作用,這很奇怪。

先感謝您

盡管我承認我不完全理解最終的工作代碼,但我還是希望分享一個工作版本以供將來參考。

數據庫配置文件:

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

帶有查詢的DBHelper文件:

導入pymysql導入dbconfig

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('/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表單如上所示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM