5

Based on a few examples (here, here) I can use psycopg2 to read and hopefully run a SQL file from python (the file is 200 lines, and though runs quickly, isn't something I want to maintain in a python file).

Here is the script to read and run the file:

sql = open("sql/sm_bounds_current.sql", "r").read()

curDest.execute(sql)

However, when I run the script, the following error is thrown:

Error: syntax error at or near "drop"
LINE 1: drop table dpsdata.sm_boundaries_current_dev;

As you can see, the first line in the script is to drop a table, but I'm not sure why the extra characters are being read, and can't seem to find a solution that might set the encoding of the file when reading it.

DPSSpatial
  • 767
  • 3
  • 11
  • 31
  • 10:1 that file has been corrupted by a dodgy text editor. "utf8: BOM considered harmful" – Jasen Dec 08 '17 at 04:05
  • @Jasen hmm, I was just using PGAdmin3 - I'll see if I can check any other reader/writer for a better method... – DPSSpatial Dec 08 '17 at 19:54

1 Answers1

5

Found this post dealing with encoding and byte order marks, which is where my problem was.

The solution was to import OPEN from CODECS, which allows for the ENCODING option on OPEN:

import codecs
from codecs import open

sqlfile = "sql/sm_bounds_current.sql"
sql = open(sqlfile, mode='r', encoding='utf-8-sig').read()
curDest.execute(sql)

Seems to work great!

DPSSpatial
  • 767
  • 3
  • 11
  • 31