2013-02-22 156 views
1

我有以下一段代码在postgresql服务器中执行。Python 3索引超出范围错误

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

+1

您正在吞咽原始异常。出于测试目的,请在您的except语句中对其进行重新评估,以便我们看到它实际来自哪个线程。 – jknupp 2013-02-22 13:52:36

+0

我正在更新查询列表时出现异常。该控件甚至不会进入查询执行的部分。在我将查询插入到适当列表的位置时弹出错误。 – 2013-02-22 13:54:51

+2

你真的需要在'except'子句中单独调用'raise',这样我们就可以看到实际的回溯和行数。 – jknupp 2013-02-22 14:16:55

回答

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 

看到原来的回溯和错误发生的确切行。

+0

检查了它。这里有一些_aunits的错误。 --plpy.Error:生成插入查询时出错 - >列表索引超出rangeDebug是_aunits =:1300.0 - 已完成---- – 2013-02-22 13:56:12

+0

代码正在完成,直到main for循环的第一次迭代。请注意,我的测试数据中批量编码的数量仅为1。它有什么区别吗? – 2013-02-22 14:04:42

1

如果长度为0,则将ptrcode设置为1,然后在进入循环时立即更新它。也许你打算把它设置为0

+0

是的。谢谢。这是我的错误。但是,ptrcode只是表格中的一列,我也用0检查过,错误仍然存​​在。它的价值不是错误的原因。 – 2013-02-22 14:12:27