【问题标题】:Syntactical error in INSERT using psycopg2使用 psycopg2 插入中的语法错误
【发布时间】:2020-07-08 22:58:54
【问题描述】:

我正在尝试使用 psycopg2 包将一些记录插入到 POSTGRES 数据库中。

我遇到了一个我无法弄清楚的错误。

    import numpy as np
import psycopg2
#video_id, frame_num, human_num, keypoint_id, part_x, part_y, confidence
video_id = [1,1,1]
frame_id = [1,1,1]
human_num = [1,1,1]
keypoint_id = [0,1,2]
part_x = [0.33, 0.33, 0.33]
part_y = [0.66, 0.66, 0.66]
confidence = [0.34, 0.45, 0.67]

recs = np.core.records.fromarrays([video_id, frame_id, human_num,keypoint_id, part_x, part_y, confidence])
a_str = ",".join(map(str, recs))

user = 'XX'
pwd = 'XX'
host = 'XX'
port = 'XX'
database = 'XX'

try:
        connection = psycopg2.connect(user = user,
                                  password = pwd,
                                  host = host,
                                  port = port,
                                  database = database)
        #print ("CONN: Established Connection")
        cursor = connection.cursor()

        # Get the data in the required format
        data_sql =  a_str
        #print ("CONN: Converted to recs")

        # Prepare the INSERT Query  to insert keypoints into pose_data
        records_list_template = ','.join('%s' * len(data_sql))
        #print ("CONN: records_template", records_list_template)
        insert_query = 'INSERT INTO pose_data (video_id, frame_num, human_num, keypoint_id, part_x, part_y, confidence) VALUES %s;'
        print ("CONN: Insert Query is ", insert_query)
        # Execute the query
        #print("CONN: Writing ", data_sql)
        cursor.execute(insert_query, [data_sql])
except (Exception, psycopg2.Error) as error :
        print ("Error in PostgreSQL try block:", error)

finally:
    #closing database connection.
        if(connection):
            cursor.close()
            connection.close()
            print("PostgreSQL connection is closed")

我有以下输出(包含错误)

('CONN: Insert Query is ', 'INSERT INTOpose_data (video_id, frame_num、human_num、keypoint_id、part_x、part_y、置信度)值 %s;') ('PostgreSQL try 块中的错误:', ProgrammingError('syntax "\'(1, 1, 1, 0, 0.33, 0.66, 0.34),(1, 1, 1, 1, 0.33, 0.66, 0.45),(1, 1, 1, 2, 0.33, 0.66, 0.67)\'"\nLINE 1: ..., keypoint_id, part_x, part_y, confidence) 值 \'(1, 1, 1,. ..\n
^\n',))

【问题讨论】:

    标签: psycopg2


    【解决方案1】:

    变量 %s 不会被它在语句中的值替换

    insert_query = 'INSERT INTO pose_data (video_id, frame_num, human_num, keypoint_id, part_x, part_y, confidence) VALUES %s;'

    【讨论】:

    • 我认为它在下一条语句中被替换:cursor.execute(insert_query, [data_sql])。不是吗?
    • 所以我们应该看看 data_sql 中的值 .. 你能取消注释 #print("CONN: Writing ", data_sql) 并发布输出吗?
    • ('CONN: Insert Query is ', 'INSERT INTO pose_data (video_id, frame_num, human_num, keypoint_id, part_x, part_y, confidence) VALUES %s;') ('CONN: Writing ', ' (1, 1, 1, 0, 0.33, 0.66, 0.34),(1, 1, 1, 1, 0.33, 0.66, 0.45),(1, 1, 1, 2, 0.33, 0.66, 0.67)') ( 'PostgreSQL 尝试块中的错误:', ProgrammingError('在 "\'(1, 1, 1, 0, 0.33, 0.66, 0.34),(1, 1, 1, 1, 0.33, 0.66, 0.45) 处或附近出现语法错误),(1, 1, 1, 2, 0.33, 0.66, 0.67)\'"\nLINE 1: ..., keypoint_id, part_x, part_y, confidence) VALUES \'(1, 1, 1,...\ n ^\n',))
    • 似乎是执行语句中的错误。一个简单的解决方法是循环遍历 data_sql 中的所有项目并为每个项目运行查询。速度较慢,但​​至少它应该可以工作。
    • 我错了,它在data_sql的开头:还有一个额外的',' 条目。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    相关资源
    最近更新 更多