即使文件可用,也会出现文件位置问题

时间:2018-06-05 06:56:45

标签: python postgresql python-2.7

我在python中有以下几行代码。我必须从表复制并将其传递给给定的文件位置。我有一个名为distance.txt的文件,但由于文件已在该位置可用,因此找不到错误文件。

任何人都能说出我做错了什么。

cur.execute(("""COPY (select source, target, sum(cost)/1000 as cost from dm where source != 88888888 and target != 88888888 group by source, target order by source) TO '%s\\distance.txt'""") % (os.getcwd()))
con.commit()

2 个答案:

答案 0 :(得分:0)

尝试:

import os
filepath = os.path.join(os.getcwd(), 'distance.txt')

cur.execute("""COPY (select source, target, sum(cost)/1000 as cost from dm where source != 88888888 and target != 88888888 group by source, target order by source) TO '%s'""" % (filepath))
con.commit()

答案 1 :(得分:0)

来自DB-API 2.0 interface for SQLite databases文档:

# Never do this -- insecure!
symbol = 'RHAT'
c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)

# Do this instead
t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
print(c.fetchone())

将该规则应用于您的问题,请将我们带到这里:

import os
filepath = os.path.join(os.getcwd(), 'distance.txt')

cur.execute("COPY (Select * FROM SomeTable) TO ?", (filepath, ))
con.commit()