有一个TypeError:在插入mysql表时,execute()最多接受3个参数(给定6个) [英] There is an TypeError: execute() takes at most 3 arguments (6 given) when inserting into mysql table

查看:247
本文介绍了有一个TypeError:在插入mysql表时,execute()最多接受3个参数(给定6个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在python中制作了一个webscraper,我现在正试图使其输出将我抓取的数据输出到mysqldb中,但是我一直收到此错误,而且我不知道如何解决它.这就是我的错误得到

I have made a webscraper in python and I am now trying to get it to output the data i have scraped into a mysqldb but I keep getting this error and I don't know how to fix it.this is the error I get

 File "C:/Users/owner/Downloads/Scrape (2).py", line 16, in <module>
    cursor.execute("INSERT INTO london10dayforecast (Day,Condition,High,Low,) VALUES (?,?,?,?)", str(var1),str(var2),str(var3),str(var4))
TypeError: execute() takes at most 3 arguments (6 given)

Process finished with exit code 1

这是代码:

import requests
import MySQLdb

r = requests.get("http://api.wunderground.com/api/be6eb0a1b404829b/forecast10day/q/UK/London.json")
data = r.json()

for day in data['forecast']['simpleforecast']['forecastday']:
var1= day['date']['weekday'] + ":"
var2= "Conditions: ", day['conditions']
var3= "High: ", day['high']['celsius'] + "'C"
var4= "Low: ", day['low']['celsius'] + "'C", '\n'

db = MySQLdb.connect("127.0.0.1","root","","weathersystem" )
cursor = db.cursor()

cursor.execute("INSERT INTO london10dayforecast (Day,Condition,High,Low,) VALUES (?,?,?,?)", str(var1),str(var2),str(var3),str(var4))
db.commit()
db.close()

``

推荐答案

您需要将值作为元组传入

You need to pass the values in as a tuple

cursor.execute("INSERT INTO london10dayforecast (Day,Condition,High,Low) VALUES (%s,%s,%s,%s)", (str(var1),str(var2),str(var3),str(var4)))

或更可读

qstr = "INSERT INTO london10dayforecast (Day,Condition,High,Low) VALUES (%s,%s,%s,%s)"
params = str(var1), str(var2), str(var3), str(var4)
cursor.execute(qstr, params)

这篇关于有一个TypeError:在插入mysql表时,execute()最多接受3个参数(给定6个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆