gpt4 book ai didi

python - 如何用Python从mysql数据库中获取数据

转载 作者:行者123 更新时间:2023-11-30 22:57:47 25 4
gpt4 key购买 nike

<分区>

我是 python 的新手,当我在 mysql 查询中传递参数时,我在从 mysql 数据库获取数据时遇到问题,我认为我的 mysql 语法不正确。

这是屏幕上显示的错误。

内部服务器错误

服务器遇到内部错误或配置错误,无法完成您的请求。

请通过 webmaster@localhost 联系服务器管理员,告知他们发生此错误的时间,以及您在此错误之前执行的操作。

有关此错误的更多信息可在服务器错误日志中找到。Apache/2.4.6 (Ubuntu) 服务器在 localhost 端口 80

这是我的 Select 查询代码,我想从 Url 的 get 参数中获取数据。

   #!/usr/bin/python2.7

import cgi;
import cgitb; cgitb.enable();
import time, calendar;

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()

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com