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
cur.rowcount()should have the returned number of rows. Check if it's greater than 0 (no rows).cur.rowcountequals -1 whether the row count is 0 or 1.res.rowcount()?