【发布时间】:2014-10-18 05:42:09
【问题描述】:
我是 python 新手,当我在 MySQL 查询中传递参数时从 MySQL DB 获取数据时遇到问题,我认为我的 MySQL 语法不正确。
这是屏幕上显示的错误,如下所示。
内部服务器错误
服务器遇到内部错误或配置错误,无法完成您的请求。
请通过 webmaster@localhost 联系服务器管理员,告知他们此错误发生的时间,以及您在此错误之前执行的操作。
服务器错误日志中可能提供有关此错误的更多信息。 Apache/2.4.6 (Ubuntu) 服务器在 localhost 端口 80
这是我的 Select 查询代码,我想从 Url 的 get 参数中获取数据。
#!/usr/bin/python2.7
import cgi
import cgitb
cgitb.enable()
print "Content-type: text/html\n\n"
print "<h1>Hello Python</h1>"
#!/usr/bin/python
import MySQLdb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
first_name = form.getvalue('first_name')
last_name = form.getvalue('last_name')
# Open database connection
db = MySQLdb.connect("localhost","root","123456789","testdrive" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Prepare SQL query to INSERT a record into the database
sqlstmt = "SELECT * FROM EMPLOYEE WHERE FIRST_NAME = %(first_name)s AND LAST_NAME = %(last_name)s"
try:
# Execute the SQL command
cursor.execute(sqlstmt, {'first_name': first_name, 'last_name': last_name})
# Fetch all the rows in a list of lists.
results = cursor.fetchall()
for row in results:
fname = row[0]
lname = row[1]
age = row[2]
sex = row[3]
income = row[4]
# Now print fetched result
print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
(fname, lname, age, sex, income )
except:
print "Error: unable to fecth data"
# disconnect from server
db.close()
【问题讨论】:
-
请任何人帮助我