我有一个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')
文本文件如下所示:
当我运行这个脚本时,我没有得到错误,但是数据库表没有得到任何数据。这个表一开始是空的。会少什么?在
也许吧? -g
相关问题 更多 >
编程相关推荐