0

Below is a code for python server pages(PSP); using mysqldb, I am trying to fetch records from the table "addr" and then print the same onto an html page.

But for some reason the print statement inside the for loop (containing table rows) and outside the for loop(containing "Hello" statement) isn't working due to which after the code execution at last only the statement "Rows printed" is displayed and nothing else.

<% import MySQLdb 

conn = MySQLdb.Connect(host='localhost',user='user',passwd='password',db='db1')
cur = conn.cursor()
sql= "SELECT * from addr;"
try:
  cur.execute(sql)
  data = cur.fetchall()
  for row in data:
    print row[1]
  print("Hello")
  cur.close()
  conn.close()
%>
<br />
<html>
<h1> Rows Printed</h1>
</body>
</html>
<%
except MySQLdb.IntegrityError,message:
  errorcode = message[0]
%>
<html>
<body><h1> Technical issue</h1>
</body>
</html>

The print statement doesnt display the results even without the try catch block, but the same when run as a Python script works fine and provides the desired records stored in the table.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sumit Jindal
  • 13
  • 1
  • 5

1 Answers1

0

I can't for the life of me understand why you'd want to use Python Server Pages, but since you are, you obviously can't use print. You need to actually output the results to the page in PSP syntax. Something like:

for row in data: %>
<br><%= row[1] =%>
<%
cur.close()

etc.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks for the input Daniel; it worked very well. But from your post another doubt arose in my mind i.e. Is the usage of python server pages for building a website isnt recommended? From my research on internet i found that its even faster than CGI handlers but still if i am missing out any disadvantages or drawback of using PSP, request you to kindly point it out. Thanks again!!! – Sumit Jindal May 29 '12 at 13:13
  • PSP is based on mod_python, which is not under active development and is deprecated. And PSP generally is not a very nice technology - it mixes business logic and display, making development and maintenance hard. Use a framework like Django or Flask on top of WSGI - apart from proper separation of concerns and a decent template language, something like Django also includes a db layer which avoids the need for writing SQL statements. – Daniel Roseman May 29 '12 at 13:41