2017-08-09 87 views
0
conn = psycopg2.connect("dbname=name host=host user=user password=pass port=port") 
cur = conn.cursor() 
with open('big shot.json') as f: 
    data = json.load(f) 
for key in data["permissions"]: 
    cur.execute("INSERT INTO permissions (name) VALUES (%s);", (key,)) 
conn.commit() 
output = cur.execute("SELECT * FROM permissions") 
print(output) 

我有這個,我試圖用來在我的數據庫中創建新行,但它什麼都不做。它不會返回任何錯誤,但它也不會寫入到我的數據庫中,顯然,輸出在控制檯中返回「無」。用python插入Postgres DB列

回答

2

您需要從遊標中讀取數據:

cur.execute("SELECT * FROM permissions") 
data = cur.fetchall() 
print(data) 
+0

好吧,我覺得愚蠢的,我這樣做,它的輸出一堆次因爲我已經運行了同樣的事情,這太多次LMAO – unironicallyironic