我有以下代码将文件名插入名为'logs'的Postgres表
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()
我收到错误
ProgrammingError: syntax error at or near "%"
LINE 1: INSERT INTO logs(filename) VALUES (%(fname)s)
我可以知道我做错了吗?
答案 0 :(得分:0)
您尚未将任何参数传递给查询,因此不会进行替换;适配器将文字字符串(%(fname)s)
传递给Postgres。
cur.execute("""INSERT INTO logs(filename) VALUES (%(fname)s)""", {'fname': fname'})