0

我有旨在總結表和列這條SQL語句:如何解決「IndexError:元組索引超出範圍」?

p_id p_name  atc_code   tax_amt base_amt date 
2300 |A student |WC158 - EWT 1% |133.93 |13392.86 |2015-07-01 
2300 |A student |WC158 - EWT 1% |62.50 |6250.00 |2015-07-01 
901 |B student |WC158 - EWT 1% |8.31  |830.58  |2015-06-09 
2831 |C student |WC160 - EWT 2% |2736.84 |136842.11 |2015-06-04 
905 |D student |WC158 - EWT 1% |31.25 |3125.00 |2015-06-16 
905 |D student |WC158 - EWT 1% |31.25 |3125.00 |2015-06-29 
905 |D student |WC158 - EWT 1% |31.25 |3125.00 |2015-06-29 
905 |D student |WC158 - EWT 1% |26.79 |2678.57 |2015-06-16 
959 |G student |WC158 - EWT 1% |114.29 |11428.57 |2015-01-10 
959 |G student |WC158 - EWT 1% |478.90 |47890.18 |2015-01-20 
2424 |L student |WC158 - EWT 1% |45.54 |4553.58 |2015-03-03 

我也有這個說法。

... 
cr.execute('''insert into student_resource_report_line(partner_id,seq,base_amount,tax_amount,percent,atc_code,nature,create_date,write_date) 
     select es.partner_id as partner_id, 
      (case when es.name like '%WC158%' then 1 
       when es.name like '%WC160%' then 2 
       when es.name like '%WC010%' then 3 
       when es.name like '%WC140%' then 4 
       else 0 end) as seq, 
      sum(es.base_amt) as base_amount, 
      sum(es.tax_amt) as tax_amount, 
      (case when es.name like '%EWT 1%%' then '1.00' 
       when es.name like '%EWT 2%%' then '2.00' 
       when es.name like '%EWT 3%%' then '3.00' 
       when es.name like '%EWT 4%%' then '4.00' 
       when es.name like '%EWT 5%%' then '5.00' 
       when es.name like '%EWT 6%%' then '6.00' 
       when es.name like '%EWT 7%%' then '7.00' 
       when es.name like '%EWT 8%%' then '8.00' 
       when es.name like '%EWT 9%%' then '9.00' 
       when es.name like '%EWT 10%%' then '10.00' 
       else null end) as percent, 
      (case when es.name like '%WC158%' then 'WC158' 
       when es.name like '%WC160%' then 'WC160' 
       when es.name like '%WC010%' then 'WC010' 
       when es.name like '%WC140%' then 'WC140' 
       else null end) as atc_code, 
      (case when es.name like '%WC158%' then 'NOTEBOOK' 
       when es.name like '%WC160%' then 'BACKPACK' 
       when es.name like '%WC010%' then 'COLOR' 
       when es.name like '%WC140%' then 'BOOKS' else null end) as nature, 
      (now()) as create_date,(now()) as write_date 
     from ewt_source es where es.date between ? and ? 
     group by es.partner_id,es.name''',(ewt.date_from,ewt.date_to)) 

其中ewt.date_fromewt.date_to是從用戶的輸入。我無法理解的是在執行期間(當我的視圖調用這個方法時)它會產生一個錯誤:「IndexError:元組索引超出範圍」。這是我在我的日誌中總是看到的,並且還表示我的查詢很糟糕。

group by es.partner_id,es.name''',(ewt.date_from,ewt.date_to)) 
File "/opt/openerp/server-7/openerp/sql_db.py", line 161, in wrapper 
     return f(self, *args, **kwargs) 
File "/opt/openerp/server-7/openerp/sql_db.py", line 226, in execute 
    res = self._obj.execute(query, params) 
IndexError: tuple index out of range 

有誰能夠指出來哪裏做我犯的一個錯誤?它真的旋轉我的頭。

回答

0

嘗試更換您的'?'在'openerp sql-s'中也使用'%s'。

也許嘗試你的SQL格式化成類似的東西:

cr.execute('SELECT id FROM account_move_line \ 
      WHERE state = %s \ 
      AND company_id in (%s) \ 
      ORDER BY id DESC ',\ 
      ('valid', company_ids)) 

或可能:

self.cr.execute("SELECT sum(debit-credit) " \ 
       "FROM account_move_line AS l " \ 
       "JOIN account_move am ON (am.id = l.move_id)" \ 
       "WHERE l.account_id IN %s" \ 
       "AND am.state IN %s", 
       (tuple(self.account_ids), tuple(move_state),)) 

希望它可以幫助隊友!

0

刪除?從查詢標誌改用%s的,最後刪除,%(ewt.date_from,ewt.date_to))

總之查詢看起來像替換它,

qry = "select * from table where field1 = %s and field2=%s" %(value1, value2) 
cr.execute(qry) 
0

我有這個令人沮喪的錯誤。 所以下面可能是與查詢的問題: -

1)當你有like你的說法確保有2個跡象 即like %%WC158%%所以正在執行時,這相當於%WC158%

2 )比如你有

es.date between ? and ? 
        group by es.partner_id,es.name''' 
在你的代碼

嘗試使用%s代替?

3)這部分代碼

''',(ewt.date_from,ewt.date_to))''' 

確保您在結束

,(ewt.date_from,ewt.date_to,)) 

cur.execute("INSERT INTO foo VALUES (%s)", "bar") # WRONG

cur.execute("INSERT INTO foo VALUES (%s)", ("bar")) # WRONG

cur.execute("INSERT INTO foo VALUES (%s)", ("bar",)) # correct

cur.execute("INSERT INTO foo VALUES (%s)", ["bar"]) # correct

source

有一個額外的逗號