使用Python CGI Framework从mysql表中检索数据

时间:2019-06-19 15:51:44

标签: python html mysql apache2

我正在尝试使用Python CGI Framework从MySQL数据库检索数据,同时将该程序托管在网络上。但是无法获得结果,而是在接受用户输入时遇到错误。

http://www.extappdynamics.com/cgi-bin/fsearch1.cgi

有人可以帮助我解决此错误吗?感谢您的帮助,谢谢。

这是使用Python CGI Framework和MySQL数据库。尝试将程序托管在apache2服务器上。操作系统-Linux Ubuntu。

fsearch1.py

#!/usr/bin/env python
import sqlite3 as sq
import mysql.connector as mx
import sys
import cgi
import cgitb
cgitb.enable()


def htmltop():
    print("""Content-type: text/html\r\n\r\n
    <!DOCTYPE html><html><body>
    <table border='1'><tr><th>EMPLOYEE NUMBER</th><th>BIRTHDAY</th><th>FIRST NAME</th><th>LAST NAME</th><th>GENDER</th><th>HIRE DATE</th>
    <th>DEPARTMENT NAME</th><th>DEPARTMENT NUMBER</th></tr>""")

def htmltail():
    print("""</body></html>""")

def connectdb():
    mydb=mx.connect(host='localhost',user='******',passwd='*******',database='emp')
    cur=mydb.cursor()
    return mydb,cur

def retrieve(mydb,cur):
    form = cgi.FieldStorage()
#       print (form['valuetosearch'])
    if form.getvalue('valuetosearch'):
#               valuetosearch = form.getvalue("valuetosearch")
            query = "SELECT * from emptbl WHERE (empno || bday || fname || lname || gender || hdate || depname || depno LIKE ?)"
            result = cur.execute(query,search).fetchall()
            #form.getvalue['valuetosearch'])
            #result=cur.fetchall()
            print(result)
            print("here")
            return result
            print("connection established")
    else:
            print("Enter to search based on the below table:<br><br>")
    #cur.execute(query)
    #result=cur.fetchall()
    #return result

def displaytest(result):
    print("<table>")
    for x in result:
            print("<tr>")
            print("<td> {0} </td>".format(x[0]))
            print("<td> {0} </td>".format(x[1]))
            print("<td> {0} </td>".format(x[2]))
            print("<td> {0} </td>".format(x[3]))
            print("<td> {0} </td>".format(x[4]))
            print("<td> {0} </td>".format(x[5]))
            print("<td> {0} </td>".format(x[6]))
            print("<td> {0} </td>".format(x[7]))
            print("</tr>")
    print("</table>")

if __name__ == '__main__':
    try:
            htmltop()
            print("<form action = '/cgi-bin/fsearch1.py' method = 'POST'>")
            #print('<form action="" method="POST" enctype="multipart/form-data">')
            print('<input type="text" name="valuetosearch" placeholder="Search">')
            print('<input type="submit" name="search" value="Search"><br><br>')
            print('</form>')
            mydb,cur=connectdb()
            result=retrieve(mydb,cur)
            displaytest(result)
            cur.close()
            htmltail()
    except:
            cgi.print_exception()

我期望结果是类似于PHP的结果。 http://www.extappdynamics.com/cgi-bin/fsearch1.php

但是当我尝试编译程序时-这是我得到的错误

<H3>Traceback (most recent call last):</H3>
<PRE>  File "fsearch1.cgi", line 71, in &lt;module&gt;
displaytest(result)
  File "fsearch1.cgi", line 48, in displaytest
for x in result:
<B>TypeError: 'NoneType' object is not iterable

0 个答案:

没有答案