Python MySQL连接/查询使文件无法工作
我正在编写一些测试代码。在一个单独的HTML文件中,button onclick事件获取页面的URL,并将其作为变量jquery_输入传递给这个python脚本。Python然后刮取URL并标识两个数据段,然后对它们进行格式化并连接在一起,从而生成变量lowerCaseJoined。这个连接的变量在MySQL数据库中有一个对应的条目。数据库中的每个条目都有一个相关的.gif文件 从这里开始,我要做的是打开到MySQL服务器的连接,并根据db查询连接的变量,以获得相关的.gif文件 完成后,我想将.gif文件打印为网页上的警报 如果我取出代码连接的db部分,查询,代码运行正常。此外,我还能够通过pythonshell成功地独立执行代码的db部分。但是,当整个代码驻留在一个文件中时,单击按钮时不会发生任何事情。我已经系统地删除了与db连接相关的代码行,并且我的代码在第一行db=MySQLdb.connection….处开始暂停。。。。所以,我一开始尝试连接数据库,程序就启动了 代码如下:Python MySQL连接/查询使文件无法工作,python,mysql,Python,Mysql,我正在编写一些测试代码。在一个单独的HTML文件中,button onclick事件获取页面的URL,并将其作为变量jquery_输入传递给这个python脚本。Python然后刮取URL并标识两个数据段,然后对它们进行格式化并连接在一起,从而生成变量lowerCaseJoined。这个连接的变量在MySQL数据库中有一个对应的条目。数据库中的每个条目都有一个相关的.gif文件 从这里开始,我要做的是打开到MySQL服务器的连接,并根据db查询连接的变量,以获得相关的.gif文件 完成后,我想将
#!/usr/bin/python
from bs4 import BeautifulSoup as Soup
import urllib
import re
import cgi, cgitb
import MySQLdb
cgitb.enable() # for troubleshooting
# the cgi library gets the var from the .html file
form = cgi.FieldStorage()
jquery_input = form.getvalue("stuff_for_python", "nothing sent")
# the next section scrapes the URL,
# finds the call no and location,
# formats them, and concatenates them
content = urllib.urlopen(jquery_input).read()
soup = Soup(content)
extracted = soup.find_all("tr", {"class": "bibItemsEntry"})
cleaned = str(extracted)
start = cleaned.find('browse') +8
end = cleaned.find('</a>', start)
callNo = cleaned[start:end]
noSpacesCallNo = callNo.replace(' ', '')
noSpacesCallNo2 = noSpacesCallNo.replace('.', '')
startLoc = cleaned.find('field 1') + 13
endLoc = cleaned.find('</td>', startLoc)
location = cleaned[startLoc:endLoc]
noSpacesLoc = location.replace(' ', '')
joined = (noSpacesCallNo2+noSpacesLoc)
lowerCaseJoined = joined.lower()
# the next section establishes a connection
# with the mySQL db and queries it
# using the call/loc code (lowerCaseJoined)
db = MySQLdb.connect(host="localhost", user="...", "passwd="...",
db="locations")
cur = db.cursor()
queryDb = """
SELECT URL FROM locations WHERE location = %s
"""
cur.execute(queryDb, lowerCaseJoined)
result = cur.fetchall()
cur.close()
db.close()
# the next 2 'print' statements are important for web
print "Content-type: text/html"
print
print result
知道我做错了什么吗
我是编程新手,所以我相信这里有很多可以改进的地方。但在改进它之前,我只想让它工作起来 我解决了这个问题。似乎在db连接线的密码部分之前有引号。现在一切都好了。您是否尝试过在代码中有问题的部分添加打印语句,例如在db=。。。行和cur=。。。行和cur.execute。。。只是为了看看代码能走多远?