1
我對python非常陌生,這是我的第一個編程語言。這是我第一次使用SQL以及psycopg2。任何「愚蠢」的建議非常感謝!Psycopg2執行元組索引超出範圍
我不確定是什麼問題。我的研究告訴我,我只是給cursor.execute(INSERT...
提供了太多或太多的論據,但我已經嘗試了許多不同的計數,並且似乎無法正確工作。從我的角度來看,cursor.execute(CREATE...
創建了一個有6列的表格,我正在向它傳遞6個參數。
from lxml import html # Used to parse XML
import requests #used to service API request
itemtypeid1 = 34
itemtypeid2 = 35
regionid = 10000002
webpage = requests.get('http://api.eve-central.com/api/marketstat?typeid=%i&typeid=%i®ionlimit=%i' % (
itemtypeid1, itemtypeid2, regionid))
if webpage.status_code == 200:
data = html.fromstring(webpage.content)
for item in data.iter('type'):
buy_dict = {node.tag: node.text for node in item.xpath("buy/*")}
sell_dict = {node.tag: node.text for node in item.xpath("sell/*")}
#Variables for output
itemid = (item.get("id"))
buymin = buy_dict['min']
buymax = buy_dict['max']
buymedian = buy_dict['median']
buyvolume = buy_dict['volume']
buyaverage = buy_dict['avg']
#Fail if api webpage unavaliable
else:
print "Webpage unavaliable"
Webpage.raise_for_status()
#############################################################################
import psycopg2
connection = psycopg2.connect(database='evemarketdata', user='postgres', password='black3car')
#open a cursor to perform DB operations
cursor = connection.cursor()
#create new table
cursor.execute("CREATE TABLE arkonor (itemid integer primary key, min integer, max integer, median integer, volume integer, average integer);")
#Insert row data into DB table
cursor.execute("""INSERT INTO arkonor (typeid, min, max, median, volume, average)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)""",
('itemid', 'buymin', 'buymax', 'buymedian', 'buyvolume', 'buyaverage'))
#Commits all changes does with cursor
#connection.commit()
結果
Traceback (most recent call last):
File "E:\Eve Spreadsheets\Python\PostgreConnect.py", line 49, in <module>
('itemid', 'buymin', 'buymax', 'buymedian', 'buyvolume', 'buyaverage'))
IndexError: tuple index out of range
只是經過並想提醒你:在「INSERT」的執行語句中,實際上是將6個字符串傳遞給查詢。您需要傳遞變量的值(即不帶引號) – Flickerlight