0

I have a got the path to a csv file using

cur1.execute("select path from t2");
path = cur1.fetchone()

I know want to perform a copy to the csv file using psycopg2 which goes like :

cur2.execute("COPY (SELECT id,a,b,c FROM t1,t2 WHERE a >= t2.d ) TO path[0] DELIMITER ',' CSV;")

I am getting this error: psycopg2.ProgrammingError: syntax error at or near "path"

Thanks in advance!

2
  • You expect psql to access your python variables ? Commented Jun 15, 2016 at 14:25
  • I added the psql tag wrongly, which I have removed. Commented Jun 15, 2016 at 14:28

1 Answer 1

1

There is actually two problems here.

The first is you have to surround your filename with ', that's what the error message indicates.

The second is that if you do that, your file will be named path[0], which is probably not what you want, because your database can't know what path is and access it (that's what I tried to say). So you have to insert the filename in the query before sending it to your db. see http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.