簡體   English   中英

使用python時的PostgreSQL插入數據錯誤

[英]Postgresql insert data error when using python

我試圖將數據插入到先前使用python腳本創建的表中。 這是我嘗試執行的代碼。 我也想將數據插入日期表中。

date_today = dt.date.today()

conn = psycopg2.connect(host = serverip, port = port, database = database,    user = uid, password = pwd)
cursor = conn.cursor()

cursor.execute("INSERT INTO My_TABLE (Date, Class, Total_students, failed_students, Percent_passed_students) VALUES (date_today, 'Class Name', int1, int2, int3)")

print "Data Inserted successfully"
conn.commit()
conn.close()

這是我從工作中看到的錯誤。 我在這里想念什么?

psycopg2.ProgrammingError: column "date_today" does not exist

我通過以下查詢使用其他作業創建了表:

cursor.execute("""CREATE TABLE MY_TABL(Date date, Lob varchar(30), Total_Students int, failed_students int, Percent_passed_students int)""")

並使用上述五列創建該表。

這行:

cursor.execute("INSERT INTO My_TABLE (Date, Class, Total_students, failed_students, Percent_passed_students) VALUES (date_today, 'Class Name', int1, int2, int3)")

將值動態插入數據庫的錯誤方法。


這是一個功能正確的示例:

cursor.execute("INSERT INTO table VALUES (%s, %s, %s)", (var1, var2, var3))

並在您的情況下應用...

cursor.execute("INSERT INTO My_TABLE VALUES (%s, %s, %s, %s, %s)", (date_today, 'Class Name', int1, int2, int3))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM