Skip to content

Commit 78ad8e5

Browse files
committed
basic exception handling
1 parent 3a410ac commit 78ad8e5

1 file changed

Lines changed: 23 additions & 19 deletions

File tree

worldcities.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,29 @@
2525
# handle database request and query city information
2626
def city(name=None):
2727
# connect to DB2
28-
db2conn = ibm_db.connect(db2cred['ssldsn'], "","")
29-
if db2conn:
30-
# we have a Db2 connection, query the database
31-
sql="select * from cities where name=? order by population desc"
32-
# Note that for security reasons we are preparing the statement first,
33-
# then bind the form input as value to the statement to replace the
34-
# parameter marker.
35-
stmt = ibm_db.prepare(db2conn,sql)
36-
ibm_db.bind_param(stmt, 1, name)
37-
ibm_db.execute(stmt)
38-
rows=[]
39-
# fetch the result
40-
result = ibm_db.fetch_assoc(stmt)
41-
while result != False:
42-
rows.append(result.copy())
43-
result = ibm_db.fetch_assoc(stmt)
44-
# close database connection
45-
ibm_db.close(db2conn)
46-
return render_template('city.html', ci=rows)
28+
rows=[]
29+
try:
30+
db2conn = ibm_db.connect(db2cred['ssldsn'], "","")
31+
if db2conn:
32+
# we have a Db2 connection, query the database
33+
sql="select * from cities where name=? order by population desc"
34+
# Note that for security reasons we are preparing the statement first,
35+
# then bind the form input as value to the statement to replace the
36+
# parameter marker.
37+
stmt = ibm_db.prepare(db2conn,sql)
38+
ibm_db.bind_param(stmt, 1, name)
39+
ibm_db.execute(stmt)
40+
# fetch the result
41+
result = ibm_db.fetch_assoc(stmt)
42+
while result != False:
43+
rows.append(result.copy())
44+
result = ibm_db.fetch_assoc(stmt)
45+
# close database connection
46+
ibm_db.close(db2conn)
47+
return render_template('city.html', ci=rows)
48+
except:
49+
return "Db2 connection issues"
50+
4751

4852
# main page to dump some environment information
4953
@app.route('/')

0 commit comments

Comments
 (0)