如何在python CGI中从db打印值

时间:2016-03-08 17:19:10

标签: python mysql cgi

我是初学者并且不断尝试通过cgi-bin在网页上打印价值,但我不知道我做错了什么。我经历了不同的教程,如:

http://www.inmotionhosting.com/support/website/python/how-to-use-python-to-connect-to-a-database

http://ianhowson.com/a-quick-guide-to-using-mysql-in-python.html

http://www.tutorialspoint.com/python/python_database_access.htm 和一些线程一样:Returning Output of Python CGI MySQL Script因为我认为我在某处做错了但是没有打印。

这是我的代码,只有打印 "hello"

#!C:\Python27\python.exe

import cgi
import cgitb

print "Content-type: text/html\n"
print """
<html>
<head>
<p> hello</p>
</head>
"""

import MySQLdb

db = MySQLdb.connect(host="127.0.0.1",port=3306, user="root", passwd="",db="project")


cursor = db.cursor()
cursor2 = db.cursor()
# execute SQL select statement
#sql = "SELECT stid,firstname FROM student WERE stid IN(SELECT stid FROM stobt WHERE stid=%s" % (0)

sql2 = "SELECT stid FROM student  WHERE firstname = '%s'" % ('User')


# Execute the SQL command

cursor2.execute(sql2)
# Fetch all the rows in a list of lists.

result2 = cursor2.fetchall()
#print result2

row2 = result2[0]
#print 'r1',row[1]
#print 'r2',row2[0]
sql = "SELECT * FROM stobt  WHERE stid = '%d'" % (row2)
cursor.execute(sql)
results = cursor.fetchall()
row = results[1]
tc = 56
for row in results:

   if row2[0] == row[1]:
      idd = row[0]
      stid = row[1]
      obtained = row[2]
      subject = row[3]

   # Now print fetched result

      print( "<h1>id=%s,stid=%s,obtained=%f,subject=%s</h1>" %(idd, stid, obtained, subject ))

print """
<body>
<p> idd</p>
</body>
</html>
"""
    #  if obtained <= 50:
     #    if tc >= 50:
      #      print tc
db.close()      

有什么想法吗?

0 个答案:

没有答案