2017-09-27 63 views
0

我正在使用python 3.6,psycopg2将数据插入postgresql(9.6)数据库。这里是我的代码:如何使用psycopg2插入列表清单

def postgre_save(): 
    params = Config() 
    with psycopg2.connect(**params) as conn: 
     cur = conn.cursor() 
     lst2 = [] 
     lst2.append([9999, datetime.date(2017, 5, 1), 0.99, 1, 3]) 
     lst2.append([9999, datetime.date(2017, 6, 1), 1.2, 1, 3]) 
     qry = 'INSERT INTO oww.welldep(well_id, dep_date, depletion, reach, type) VALUES (%s, %s, %s, %s, %s);' 
     cur.execute(qry, lst2) 

当这被执行时,我收到以下错误:IndexError:列表索引超出范围。

如果我缩短列表中的一个条目的程序执行,即:

lst2 = [9999, datetime.date(2017, 5, 1), 0.99, 1, 3] 

但真正的名单将在列表内数以千计的名单。任何帮助是极大的赞赏。谢谢。

回答