1

I'm having some trouble forming a conditional to check if the response to my API is null with Python. I am using Flask and SQLite3 modules. Below is a snippet of one of my resources.

class Sites(Resource):
        def get(self, store):
            print('Received a request at ADDRESS for Store ' + store )
            conn = sqlite3.connect('store-db.db')
            cur = conn.cursor()
            res = cur.execute('SELECT * FROM Sites WHERE StoreNumber like ' + store)
            for r in res:
                if r == 'null':
                    return('No data', 404)
                else:
                    column_names = ["StoreNumber", "Street", "StreetSecondary","City","State", "ZipCode", "ContactNumber", "XO_TN", "RelocationStatus", "StoreType", "Timezone"]
                    data = [r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10]]            
                    datadict = {column_names[itemindex]:item for itemindex, item in enumerate(data)}
                    return(datadict, 200)

If I make a request to a site that doesn't exist, the value returned is null, but none of my attempts of catching this are succeeding, so I'm wondering if my way of checking is incorrect with Python?

I've tried r[0] == 'null', r is None, r == 'null', r == null, and r == Null to no avail.

Successful Request

HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 333
Access-Control-Allow-Origin: *
Server: Werkzeug/0.14.1 Python/3.7.0
Date: Thu, 15 Nov 2018 15:11:03 GMT

{
  "StoreNumber": 42,
  "Street": "5000 Robinson Centre Drive",
  "StreetSecondary": "",
  "City": "Pittsburgh",
  "State": "PA",
  "ZipCode": "15205",
  "ContactNumber": "(412) 787-1330",
  "XO_TN": "(412) 249-9161",
  "RelocationStatus": "",
  "StoreType": "Dick's Sporting Goods",
  "Timezone": "Eastern"
}

Request with no data (should return 404)

HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 5
Access-Control-Allow-Origin: *
Server: Werkzeug/0.14.1 Python/3.7.0
Date: Thu, 15 Nov 2018 15:11:41 GMT

null
4
  • 1
    cur.rowcount() should have the returned number of rows. Check if it's greater than 0 (no rows). Commented Nov 15, 2018 at 15:16
  • Sadly I couldn't. Commented Nov 15, 2018 at 15:35
  • Actually @elken this won't work as cur.rowcount equals -1 whether the row count is 0 or 1. Commented Nov 15, 2018 at 15:35
  • res.rowcount()? Commented Nov 15, 2018 at 15:36

1 Answer 1

-2

cur.rowcount() should have the returned number of rows. Check if it's greater than 0 (no rows)

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

3 Comments

stackoverflow.com/questions/21829266/… The first answer in this by Martin explains why that won't work.
I ran a query on an existing site where count should have been 1 (or 0) and it returned -1. I ran a query on a non-existing site where count should be 0 (or-1), and that returned -1.
A substitute for what you're expecting here would be len(list(cur))

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.