我将读取位于PC中特定文件夹中的多个excel文件,这些文件的结构相同,然后将它们插入PostgreSQL中的DB表中。我正在读取excel文件,但是,在cur.execute
节中出现了一个错误
Excel文件示例:
还有我的剧本:
import pandas as pd
import glob
import psycopg2
import numpy
from psycopg2.extensions import register_adapter, AsIs
def addapt_numpy_float64(numpy_float64):
return AsIs(numpy_float64)
def addapt_numpy_int64(numpy_int64):
return AsIs(numpy_int64)
register_adapter(numpy.float64, addapt_numpy_float64)
register_adapter(numpy.int64, addapt_numpy_int64)
def connect_db(frame):
# database connection
print("DB opened successfully")
sqlpart(conn, frame)
def sqlpart(conn,frame):
cur = conn.cursor()
sql="""INSERT INTO actuals (timeslot,unit_id,actual)
VALUES(%s,%s,%s)
ON CONFLICT (timeslot,unit_id,actual)
DO UPDATE SET timeslot=%s, unit_id=%s,actual=%s"""
conn.commit()
values=[]
da=[]
for x in range(len(frame)):
da=frame[0][x], frame[1][x], frame[2][x]
values.append(da)
print(values)
cur.execute(sql, values)
print("everything is done")
def patika():
path = r'mypath'
all_files = glob.glob(path + "/*.xlsx")
li = []
for filename in all_files:
df = pd.read_excel(filename, index_col=None,header=None)
li.append(df)
frame = pd.concat(li, axis=0, ignore_index=True)
#print(frame)--->> [(Timestamp('2020-03-01 00:00:00'), 200, -1000),....] values that comes from excel like this
connect_db(frame)
patika()
输出:
cur.execute(sql, values)
TypeError: not all arguments converted during string formatting
您的查询有六个需要绑定的参数。正如您在注释中所验证的,绑定元组只包含三个元素:
请将您的
insert
更新为:如果您的表在
(timeslot, unit_id)
上是唯一的,那么您的insert
将是:更新 仔细阅读后,您的代码中存在多个问题:
首先,您的
conn.commit()
在执行insert
之后没有被执行其次,您正试图将
list
个tuple
对象传递给insert
执行请尝试以下方式:
相关问题 更多 >
编程相关推荐