Reputation: 113
Dear Python community,
I have encountered a problem that I can't get solved. I recently moved to a macOS Sierra Version 10.12.6.
All I am doing is connectedly to a MariaDB Mysql database. When staring the program the initial connection and response works and hence all the output is there, however when I refresh the page I get a "TypeError: 'NoneType' object is not iterable". If I restart the program in the terminal it will work again - until I refresh....
Here the code:
from dbhelper2 import DBHelper
app = Flask(__name__)
DB = DBHelper()
@app.route('/trtdtable')
def trtdtable():
try:
tabledata = DB.table_inputs()
except Exception as e:
print(e)
tabledata = None
return render_template("trtdtable.html", tabledata=tabledata)
second part (the DB helper)
import pymysql
# Connect to the database
connection = pymysql.connect(host='localhost',user='root',password='',db='test',cursorclass=pymysql.cursors.DictCursor)
class DBHelper:
def table_inputs(self):
try:
query = "SELECT PersonID, LastName, FirstName, Address, City FROM persons";
with connection.cursor(pymysql.cursors.DictCursor) as cursor:
cursor.execute(query)
return cursor.fetchall()
finally:
connection.close()
and the HTML (in "trtdtable.html"):
<table id="customers">
<tr>
<th>ID</th>
<th>Last Name Name</th>
<th>First Name</th>
<th>Address</th>
<th>City</th>
</tr>
{% for row in tabledata %}
<tr>
<td>{{ row['PersonID'] }}</td>
<td>{{ row['LastName'] }}</td>
<td>{{ row['FirstName'] }}</td>
<td>{{ row['Address'] }}</td>
<td>{{ row['City'] }}</td>
</tr>
{% endfor %}
</table>
Thank you for your help
Upvotes: 0
Views: 630
Reputation: 599856
You're closing the connection after the query. Don't do that.
Also, never ever catch and swallow all exceptions. If you didn't have that blank except
, Flask would have told you what the error was.
Upvotes: 1