2013-02-22 2 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 루프를 사용할 때 인덱스가 범위를 벗어나는 방식을 이해할 수 없습니다.

도와주세요 !!

편집 :

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

이의 결과 집합 au가 : 여기 빈리스트가 내 테이블에 테스트 데이터, unpbatches의 항목 수는 아마도 1

+1

원래 예외를 삼키고 있습니다. 테스트 목적으로, except 문에서 그것을 재사용하면 실제로 어떤 lne이 왔는지 알 수 있습니다. – jknupp

+0

쿼리 목록을 업데이트 할 시점에서 예외가 발생했습니다. 컨트롤은 쿼리가 실행되는 부분으로 이동하지 않습니다. 해당 목록에 쿼리를 삽입 할 때 오류가 표시됩니다. –

+2

실제로'except' 절에서'raise'를 호출하여 실제 추적 및 행 번호를 볼 수 있습니다. – jknupp

답변

1

au 유의 쿼리? 쿼리가 어떤 행도 반환하지 않았을 수 있습니다.


더 일반적으로, 당신은 코멘트에 jknupp의 제안을 따라

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

그냥

raise 

원래 역 추적 및 오류가 발생한 정확한 라인을 확인하기 위해 변경해야 말하기 .

+0

체크. _aunits의 흔적이있는 오류가 있습니다. --plpy.Error : insert 쿼리를 생성하는 중 오류가 발생했습니다. - rangeDebug의 목록 색인이 _aunits = : 1300.0 - Completed ---- –

+0

입니다. 코드는 for 루프의 첫 번째 반복까지 완료됩니다. 배치 코드의 수는 테스트 데이터에서 단지 1입니다. 어떤 차이가 있습니까 ?? –

1

길이가 0이면 ptrcode을 1로 설정 한 다음 루프를 시작할 때 바로 업데이트합니다. 아마도 0으로 설정하셨습니까?

+0

예. 고맙습니다. 그건 내 실수 야. 그러나 ptrcode는 테이블의 열일 뿐이므로 0으로 체크 한 후에도 오류는 계속 발생합니다. 가치는 오류의 원인이 아닙니다. –

관련 문제