psycopg2.errors.UndefinedColumn 在尝试将数据插入到PostgreSQL数据库时发生

huangapple go评论70阅读模式
英文:

psycopg2.errors.UndefinedColumn when trying to insert data into postgres database

问题

我有一个带有来自CSV文件的数据的pandas数据框架。

我想将这些数据导入到我的PostgreSQL数据库。

import pandas as pd
import psycopg2

# 导入CSV文件,创建数据框
data = pd.read_csv('my_csv.csv', delimiter=';')
df = pd.DataFrame(data)

# 准备数据(重命名/缩短列标题)
columnsFromCSV = list(df.columns)
for i in columnsFromCSV:
    columnName = i.rsplit(None, 2)[0]
    df.rename(columns={i: columnName}, inplace=True)
df.columns = df.columns.str.lower()

# 连接到数据库
conn = psycopg2.connect(
   database='database', user='postgres', password='admin', host='127.0.0.1', port='5432'
)
cursor = conn.cursor() 

# 将数据框插入数据库
for i in df.columns[1:]:
    cursor.execute('INSERT INTO counter (counterid) VALUES ({0})'.format(i))
    for j in range(365):
        cursor.execute('INSERT INTO measurements (counterid) VALUES({0})'.format(i))
conn.commit()
conn.close()

首先,我想在counter表中存储每个计数器一次,然后我想在measurements表中存储一年的每日测量数据,包括counterid,每日测量数据将在稍后插入。

这是我收到的错误消息:

Traceback (most recent call last):
  File "c:\EnergyCounter\EnergyCounter\backend\CSV_read_script.py", line 25, in <module>
    cursor.execute('INSERT INTO counter (counterid) VALUES ({0})'.format(i))
psycopg2.errors.UndefinedColumn: ERROR:  Column "counter1" does not exist 
LINE 1: INSERT INTO counter (counterid) VALUES (counter1)

我尝试过小写,引用表名,使用"%s"而不是".format"。

英文:

I have a pandas data framework which has data from a csv file.

I want to import that data into my postgres database.

import pandas as pd
import psycopg2

# Import CSV, create Data Frame
data = pd.read_csv(&#39;my_csv.csv&#39;, delimiter=&#39;;&#39;)
df = pd.DataFrame(data)

# Prepare Data (Rename / Shorten Column-Headers)
columnsFromCSV = list(df.columns)
for i in columnsFromCSV:
    columnName = i.rsplit(None, 2)[0]
    df.rename(columns={i : columnName}, inplace=True)
df.columns = df.columns.str.lower()

# Connect to Database
conn = psycopg2.connect(
   database=&#39;database&#39;, user=&#39;postgres&#39;, password=&#39;admin&#39;, host=&#39;127.0.0.1&#39;, port= &#39;5432&#39;
)
cursor = conn.cursor() 

# Insert Data Frame into Database
for i in df.columns[1:]:
    cursor.execute(&#39;INSERT INTO counter (counterid) VALUES ({0})&#39;.format(i))
    for j in range(365):
        cursor.execute(&#39;INSERT INTO measurements (counterid) VALUES({0})&#39;.format(i))
conn.commit()
conn.close()

First I want to store every counter once in the counter table and after that I want to store the daily measurements of one year in the measurements table together with the counterid, the daily measurements will be inserted at a later time.

Thats the error message I'm getting:

Traceback (most recent call last):
  File &quot;c:\EnergyCounter\EnergyCounter\backend\CSV_read_script.py&quot;, line 25, in &lt;module&gt;
    cursor.execute(&#39;INSERT INTO counter (counterid) VALUES ({0})&#39;.format(i))
psycopg2.errors.UndefinedColumn: ERROR:  Column &#187;counter1&#171; does not exist 
LINE 1: INSERT INTO counter (counterid) VALUES (counter1)

Tried lower case, quoting tablenames, using "%s" instead of ".format".

答案1

得分: 0

答案是使用命名参数:https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries

现在的代码看起来是这样的,它可以正常工作:

# 将数据框插入数据库
for i in df.columns[1:]:
    cursor.execute('''INSERT INTO counter (counterid) VALUES (%(columnName)s)''', {'columnName': i})
    for j in range(365):
        cursor.execute('''INSERT INTO measurements (counterid) VALUES(%(columnName)s)''', {'columnName': i})
conn.commit()
conn.close()
英文:

The answer is to use named arguments: https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries

The code now looks like this and it works:

# Insert Data Frame into Database
for i in df.columns[1:]:
    cursor.execute(&#39;&#39;&#39;INSERT INTO counter (counterid) VALUES (%(columnName)s)&#39;&#39;&#39;, {&#39;columnName&#39;: i})
    for j in range(365):
        cursor.execute(&#39;&#39;&#39;INSERT INTO measurements (counterid) VALUES(%(columnName)s)&#39;&#39;&#39;, {&#39;columnName&#39;: i})
conn.commit()
conn.close()

huangapple
  • 本文由 发表于 2023年3月7日 22:16:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定