從PostgreSQL文檔中,如果您在不指定模式的情況下執行INSERT,則它應該是public模式。PostgreSQL架構「www」不存在?
conn = psycopg2.connect(dbname = 'orion',
host = 'localhost',
port = 5432,
user = 'earthling',
password = 'mysupersecretpassword')
sql = conn.cursor()
def INSERT(table, info, text):
date = datetime.date.today()
query = "INSERT INTO %s (info, text, date) " \
"VALUES (%s, %s, %s)" %(table, info, text, date)
sql.execute(query)
INSERT("main", "www.capecod.edu", "test")
出於某種原因,我看到以下錯誤?
psycopg2.ProgrammingError: schema "www" does not exist
這不會像書面的那樣工作。你必須自己插入字符串,因爲psycopg2不知道它是一個標識符而不是文字。 –
@CraigRinger謝謝,我沒有抓到。實際上,您可以將表名作爲參數傳遞,您只需要更加謹慎......我將編輯答案。 –
在名稱周圍添加''後,似乎只有工作,我沒有看到我的表中的條目?這是因爲我需要使用參數總是?或者,也許日期需要PostgreSQL風格而不是Python風格? – htmlfarmer