微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Postgres COPY FROM命令无法通过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')

文本文件如下所示:

95216   8802    269726  3   1350    1   2014-09-02 14:11:25.817178  I
95217   8802    269726  4   1351    1   2014-09-02 14:11:25.817178  I
95218   8802    269726  5   1352    1   2014-09-02 14:11:25.817178  I
95219   8802    269726  6   1353    1   2014-09-02 14:11:25.817178  I
95220   8802    269726  7   1354    1   2014-09-02 14:11:25.817178  I
95221   8802    269726  8   1355    1   2014-09-02 14:11:25.817178  I
95222   8802    269726  9   1356    1   2014-09-02 14:11:25.817178  I

当我运行此脚本时,我没有错误,但数据库表没有数据.这个表开头是空的.可能会遗漏什么?

解决方法:

print 'done copying'
cTo.commit()

也许?
-G

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐