Postgres COPY FROM命令无法通过Python运行

2024-09-29 22:34:00 发布

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

我有一个Python脚本,它使用psycopg2库连接到Postgres,并将表从Postgres数据库复制到文本文件(用制表符分隔)。这个很好用。然后,我尝试运行一个类似的函数,但这次将文本文件插入另一个Postgres数据库中的同一个表中。我的脚本如下:

def copyFromFile(tableName):
    try:
        cTo = None
        cTo = psycopg2.connect(database="postgres", user="postgres", password="postgres", host="localhost")
        tCursor = cTo.cursor()
        print 'here'
        io = open(tableName+'.txt', 'r')
        tCursor.copy_from(io, tableName)
        print 'done copying'
    except Exception as inst:
        print "I am unable to copy to " + tableName

#start 
copyFromFile('schema.tableName')

文本文件如下所示:

^{pr2}$



当我运行这个脚本时,我没有得到错误,但是数据库表没有得到任何数据。这个表一开始是空的。会少什么?在


Tags: toio脚本数据库postgrespsycopg2制表符print

热门问题