Hello Stackoverflow community
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". 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
All I am doing is making a request from an HTML form method to a Mysql database using PyMysql, Flask and MariaDB(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:
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:
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. Its just strange that the previously working code now no longer does.
Thank you in advance
tabledataisNone, and this results in the error you are seeing.DB.table_inputs(city)is returningNone, probably start debugging there. On a side note, in your except block, you'll probably provoke the exact same problem withtabledata = None, so you'll either want to handle that in your template, or usetabledata = []instead.DBHelpercode example, there is definitely something wrong there. Also please include the full code, there definitely is noconnectin the example you posted. From the looks of it, that lineconnection = self.connect()should probably be removed, but I'm really just guessing and judging from your other post you linked.