PostgreSQL – Psycopg2 Error at or Near %

postgresqlpython

enter image description here

Note location of cursor. At first I thought it was the lack of a semi colon, but as you can see, putting one in does not solve the problem or change the error. I compared this code to samples I found on http://initd.org/psycopg/docs/usage.html :

enter image description here

Based on this, I don't see the problem. FWIW, Pylint in ST3 has no issues with this code. thanks.

from os import getcwd, listdir
import psycopg2
from io import open

conn = psycopg2.connect("dbname=ktab user=malikarumi")
cur = conn.cursor()
path = getcwd()
filenames = listdir(path)

for filename in filenames:
with open(filename, 'rb') as f:
    f1 = f.read()
    cur.execute("INSERT INTO testable (title, content, chron_date, clock)\
    VALUES (%s, %s, %s, %s);"),
    (filename, f1, '2017-12-30', '23:59:00'),

conn.commit()
cur.close()
conn.close()

Best Answer

You have misplaced parenthesis.

with open(filename, 'rb') as f:
    f1 = f.read()
    cur.execute(
        "INSERT INTO testable (title, content, chron_date, clock) VALUES (%s, %s, %s, %s);", # remove parenthesis here, which ends the execute call
        (filename, f1, '2017-12-30', '23:59:00')
    ) # move above parenthesis here