0

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

6
  • From a quick glance I'd wager that tabledata is None, and this results in the error you are seeing. Commented Oct 26, 2017 at 13:27
  • correct TypeError: 'NoneType' object is not iterable is the error I get. unfortunately I am unsure as to why ... Commented Oct 26, 2017 at 13:39
  • I'd assume it is because DB.table_inputs(city) is returning None, probably start debugging there. On a side note, in your except block, you'll probably provoke the exact same problem with tabledata = None, so you'll either want to handle that in your template, or use tabledata = [] instead. Commented Oct 26, 2017 at 18:09
  • thank you getting closer... error coming back is 'DBHelper' object has no attribute 'connect' (not sure why, seems to be there) Commented Oct 26, 2017 at 18:33
  • Can you please double-check and fix the indentation of your DBHelper code example, there is definitely something wrong there. Also please include the full code, there definitely is no connect in the example you posted. From the looks of it, that line connection = self.connect() should probably be removed, but I'm really just guessing and judging from your other post you linked. Commented Oct 26, 2017 at 20:38

1 Answer 1

0

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:

import pymysql import dbconfig

class 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.