2016-08-18 22 views
0

我有以下代碼插入文件名Postgres的表稱爲「日誌」插入文件名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

您尚未將任何參數傳遞給查詢,因此不會進行替換;適配器會將字符串(%(fname)s)傳遞給Postgres。

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

這有效..謝謝Daniel – ckp