【问题标题】:Postgres COPY FROM command not working through PythonPostgres COPY FROM 命令无法通过 Python
【发布时间】:2014-09-18 16:08:02
【问题描述】:

我有一个 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



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

【问题讨论】:

    标签: python postgresql


    【解决方案1】:
    print 'done copying'
    cTo.commit()
    

    也许? -g

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多