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

问题描述 投票:0回答:2

我在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()
python postgresql python-2.7
2个回答
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()

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()
© www.soinside.com 2019 - 2024. All rights reserved.