mysql execute 获取多个参数,有一个TypeError:在插入mysql表时execute()最多接受3个参数(给定6个)...

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

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

Here is the code:

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)))

or more readably

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值