Python(1040,“连接过多") [英] Python (1040, 'Too many connections')
问题描述
im新手使用Python. 我有3个属性(名称,价格,图像)的Item表 我想从链接获取html并保存到表中.所以我这样做
im newbie in Python. I have Item table with 3 attributes ( name, price, image) I want to get html from link and save to table. So i do that
from books.models import Book, Author, Publisher, Item
import urllib
import gzip
from StringIO import StringIO
from BeautifulSoup import BeautifulSoup
import MySQLdb as mdb
def updateItems(request):
con = mdb.connect('localhost', 'anhnt', '12', 'db_django');
cur = con.cursor()
link = "http://pandagift.vn/i263/price1/qua-tang-doc-dao.htm"
request = urllib.urlopen(link)
output1 = open('web.txt', 'wb')
html = ""
html = request.read()
output1.write(html)
output1.close()
beautifulSoup = BeautifulSoup(html)
list_item = beautifulSoup.findAll("div", {"class":"Item_ProcNews"})
if list_item:
for item in list_item :
#lay anh san pham
image = item.findAll("img")[0]
st = str(image)
beginindex = st.find('src') + 5
endindex = st.find('jpg') + 3
if(endindex == 2):
endindex = st.find('png') + 3
if(endindex == 2):
endindex = st.find('jpeg') + 4
if(endindex == 3):
endindex = st.find('gif') + 3
if(endindex == 2):
endindex = st.find('bmp') + 3
if(endindex == 2):
endindex = st.find('JPG') + 3
itemimage = str(image)[beginindex:endindex].encode('utf-8')
#lay ten san pham
name = item.findAll("span", {"class":"item-name"})[0]
temp = name.findAll("a")[0]
#itemname = temp.get('alt').encode('utf-8')
itemname = temp.string.encode('utf-8')
#lay gia san pham
price = item.findAll("span", {"class":"price"})[0]
#temp1 = str(price)
#beginindex1 = temp1.find('price') + 7
#endindex1 = temp1.find('/span') -1
#itemprice = str(temp1)[beginindex1:endindex1]
itemprice = str(price.string)
#luu vao csdl
query = "INSERT INTO books_item(name, price, image) VALUES('"+itemname+"', '"+itemprice+"', '"+itemimage+"')"
cur.execute(query)
#print query
else:
cur.execute("INSERT INTO books_item(name, price) VALUES('Hat', '10000vnd')")
updateItems(request)
返回
Exception Type: OperationalError
Exception Value: (1040, 'Too many connections')
请告诉我为什么会发生,我可以解决它.非常感谢:)
Please tell me why its happen and can i do fix it. Thanks many :)
推荐答案
http://dev.mysql.com/doc/refman/5.0/en/too-many-connections.html
如果在尝试连接到mysqld服务器时收到太多连接错误,则意味着所有可用连接正在被其他客户端使用.
If you get a Too many connections error when you try to connect to the mysqld server, this means that all available connections are in use by other clients.
允许的连接数由max_connections系统变量控制.其默认值为100.如果需要支持更多连接,则应为此变量设置一个较大的值.
The number of connections permitted is controlled by the max_connections system variable. Its default value is 100. If you need to support more connections, you should set a larger value for this variable.
这意味着您打开的数据库连接数量超出了服务器允许的数量,而没有关闭它们.我从未在您的代码中看到对close
连接con
的调用.
This means that you have opened more connections to the database than permitted by the server without closing them. I never see calls to close
the connection con
in your code.
这篇关于Python(1040,“连接过多")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!