gpt4 book ai didi

Python 3 索引超出范围错误

转载 作者:行者123 更新时间:2023-11-29 13:05:18 24 4
gpt4 key购买 nike

我在 postgresql 服务器中执行了以下代码。

from datetime import datetime

now = str(datetime.now(None))
procurementinsertqueries=[]
priceupdatequeries = []
inventoryupdatequeries=[]
ptrcode = -1
debugcode=""

try:
unpbatches=[]
query = "select distinct(fk_procurementbatch_code) from newprocurementlist"
proclistresult = plpy.execute(query)

for rec in proclistresult:
unpbatches.append(rec["fk_procurementbatch_code"])

for batchcode in unpbatches:
ptrcode=-1
query = "select procurementtransaction_code from procurement where fk_procurementbatch_code="+str(batchcode)+" order by procurementtransaction_code desc limit 1"
ptrcoderesult = plpy.execute(query)

if len(ptrcoderesult)==0:
ptrcode=0
else:
ptrcode=ptrcoderesult[0]["procurementtransaction_code"]

query = "select * from newprocurementlist where fk_procurementbatch_code="+str(batchcode)
newproclistresult = plpy.execute(query)

for r in newproclistresult:
ptrcode+=1
_bcode = str(r["fk_procurementbatch_code"])
_pref = str(r["fk_product_ref"])
_up = str(r["unitsprocured"])
_tp = str(r["totalprice"])
_cp = str(r["costprice"])
_sp = str(r["sellingprice"])
_mrp = str(r["mrp"])
_trcode = str(ptrcode)
procurementinsertqueries.append("insert into procurement values("+_bcode+","+_pref+","+_up+","+_tp+","+_cp+","+_sp+","+_mrp+","+_trcode+")")
priceupdatequeries.append("insert into productpriceupdatelist values("+_pref+")")
_aunits = 0.0
_newunits = 0.0
query="select unitsavailable from inventory where fk_product_ref="+_pref
au = -1
au = plpy.execute(query)
_aunits=float(au[0]["unitsavailable"])
_newunits = _aunits+float(r["unitsprocured"])
inventoryupdatequeries.append("update inventory set unitsavailable="+str(_newunits)+" where fk_product_ref="+_pref)
debugcode+="--Completed--"
debugcode+="---Loop completed---"

except Exception as e:
plpy.execute("insert into log values(\'"+now+"\')")
raise plpy.error("Error generating insert queries-->"+str(e)+"Debug is "+debugcode)

try:
with plpy.subtransaction():
for qry in procurementinsertqueries:
plpy.execute(qry)
for qry in priceupdatequeries:
plpy.execute(qry)
for qry in inventoryupdatequeries:
plpy.execute(qry)
except Exception as e:
plpy.execute("insert into log values(\'"+now+": Error executing insert queries\')")
raise plpy.error("Error executing procurement updates. There could be loss of data.Please review database error log. -->"+str(e))

try:
plpy.execute("delete from newprocurementlist")
except Exception as e:
plpy.execute("insert into log values(\'"+now+": Error deleting new procurement list table after successful updates\')")
raise plpy.error("Error deleting completed procurement list. There could be duplication of data. Review error log file-->"+str(e))

try:
plpy.execute("select product_price_update_process()")
except Exception as e:
raise plpy.error("Error updating prices. "+str(e))

问题是我收到“索引超出范围”错误。附件是我得到的错误。

ERROR:  plpy.Error: Error generating insert queries-->list index out of rangeDebug is --Completed--
CONTEXT: Traceback (most recent call last):
PL/Python function "procurementlist_process", line 61, in <module>
raise plpy.error("Error generating insert queries-->"+str(e)+"Debug is "+debugcode)
PL/Python function "procurementlist_process"

当我使用 for 循环时,我无法理解索引为何超出范围。

请帮忙!!

编辑:请注意我表中的测试数据,unpbatches 中的项目数仅为 1。

最佳答案

也许 au 在这里是一个空列表:

    au = plpy.execute(query)
_aunits=float(au[0]["unitsavailable"])

au是查询的结果集吗?也许查询没有返回任何行。


一般来说,你应该按照jknupp在评论中的建议进行修改

raise plpy.error("Error generating insert queries-->"+str(e)+"Debug is "+debugcode)

只是

raise

查看原始回溯和发生错误的确切行。

关于Python 3 索引超出范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15025537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com