[英]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.