简体   繁体   中英

Inserting a file name to Postgres table

I have the following code to insert the file name to Postgres table called 'logs'

c = engine.connect()
conn = c.connection
cur = conn.cursor()

cur.execute("SELECT filename from logs" )
rows1 = cur.fetchall()
rows1 = [x[0] for x in rows1]

for root, directories, filenames in os.walk(path):
    for filename in filenames:
        fname = os.path.join(root,filename)
        if os.path.isfile(fname) and fname[-4:] == '.log':
            if fname not in rows1:
            print fname
            cur.execute(""" INSERT INTO logs(filename) VALUES (%(fname)s)""")
            conn.commit()

I am getting the error

ProgrammingError: syntax error at or near "%"
LINE 1:  INSERT INTO logs(filename) VALUES (%(fname)s)

May I know where I am doing wrong?

You haven't passed any parameters to the query, so no replacement will be made; the adapter will pass the literal string (%(fname)s) to Postgres.

cur.execute("""INSERT INTO logs(filename) VALUES (%(fname)s)""", {'fname': fname'})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM