Python copy_从不工作和不抛出错误

2024-04-20 10:27:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我的以下过程没有按预期将数据加载到表中:

def upload_data(parsed_buffer):  

    parsed_buffer.seek(0)

    try:

        con = psycopg2.connect(database=database, user=user, password=pw, host=host) 
        cur = con.cursor()
        try:
            cur.copy_from(parsed_buffer, 'staging.vcf_ht')
        except StandardError, err:
            conn.rollback()
            print("   Caught error (as expected):\n", err)

    except psycopg2.NotSupportedError as e:
        now = time.strftime("%H:%M:%S +0000", time.localtime())
        print("Copy failed at: " + now + " - " + e.pgerror)
        sys.exit(1)

    finally:

            if con:
                con.close()
                now = time.strftime("%H:%M:%S +0000", time.localtime())
                print('Finished loading data at:' + now)

在其他帖子中,他们讨论在写入后添加seek函数。这是我代码的第3行。这没用。我还查了一些其他的东西。1字符串缓冲区由制表符分隔的数据填充。2如果我将输出重定向到一个文件,并在psql中使用\copy命令,它的工作原理与广告中的一样。三。如果我写一个insert语句而不是一个字符串缓冲区,这也可以工作(但这对性能不利)。此过程以抛出任何错误而终止。在


Tags: 数据hostdatatime过程bufferseekparsed