我想一些值插入到我的數據庫Python的MySQLdb的插入類型錯誤:不是字符串格式化
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)和matchdate是DATE
這是印刷信息的一個示例:
(u'\n\xa0bestest \n9 - 16\ntryPANTS\xa0\n\n', u'\xa0Razer CS:GO Tournament', '08-12-2013')
嘗試'info = results [1] .strip(),results [2] .strip(),matchday'並嘗試@ avoid3d提到 –
將錯誤更改爲VALUES \ n(' Rasta.eon \\ n2 - 1 \\ nRasta.Xd','Razer CS:GO'in line 1「) – user3386406
ProgrammingError:(1064,」您的SQL語法有錯誤;請檢查與您的MySQL相對應的手冊服務器版本爲正確的語法使用附近'match,tournamentname,matchdate)VALUES \ n('Rasta.eon 2 - 1 Rasta.Xd','Razer C'在第1行) – user3386406