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.
-
10:1 that file has been corrupted by a dodgy text editor. "utf8: BOM considered harmful"Jasen– Jasen2017年12月08日 04:05:31 +00:00Commented Dec 8, 2017 at 4:05
-
@Jasen hmm, I was just using PGAdmin3 - I'll see if I can check any other reader/writer for a better method...Inactivated Account– Inactivated Account2017年12月08日 19:54:35 +00:00Commented Dec 8, 2017 at 19:54
1 Answer 1
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!
4 Comments
import codecs -part, so you can choose to take that out if you want :)