Python MySQLdb Insert TypeError:在字符串格式化期间不是所有参数都被转换

时间:2014-03-06 11:02:44

标签: python mysql-python

我正在尝试将一些值插入我的数据库

urls =("http://www.**********.net/?page=match")
hdr = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request(urls,headers=hdr)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)

tournament = soup.find('div',{'class':['content-body']})
match_times = tournament.find_all("div", style = "width:10%; float:left;")
match_names = tournament.find_all("div", style = "width:46%; float:left; margin-left:2%; margin-right:2%")
tournys = tournament.find_all("div", style = "width:40%; float:left; overflow:hidden;")


for element in zip(match_times, match_names, tournys):
    results = element[0].text, element[1].text, element[2].text
    matchday=datetime.strptime(results[0], "%d/%m/%y").strftime("%d-%m-%Y")
    info= results[1],results[2],matchday
    conn= MySQLdb.connect(host='localhost',user='root',passwd='',db='sport')
    c = conn.cursor()
    query = "INSERT INTO todaysmatches (match, tournament, matchdate) VALUES (%s,%s,%s)"
    c.executemany(query, info)
    conn.commit()
    conn.close()
    print  info
    print '==================='

匹配是varchar(150),锦标赛varchar(150),匹配日期是DATE

这是打印信息的示例:

(u'\n\xa0bestest \n9 - 16\ntryPANTS\xa0\n\n', u'\xa0Razer CS:GO Tournament', '08-12-2013')

1 个答案:

答案 0 :(得分:0)

尝试更改行:

# This expects a list as it's second argument, you are giving it a tuple.
c.executemany(query, info)

对此:

c.executemany(query, [info])

如果可行,那么您可能需要阅读文档,并在只需插入一行时查明是否有更好的方法来插入数据。 (几乎肯定是。)