unironicallyironic
unironicallyironic

Reputation: 73

Inserting into Postgres DB column with python

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)

I have this that I'm trying to use to create new rows in my database, but it doesn't do anything. It doesn't return any errors, but it also doesn't write to my database, and output, obviously, returns "None" in the console.

Upvotes: 0

Views: 49

Answers (1)

Alex
Alex

Reputation: 1262

You need to fetch the data from the cursor:

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

Upvotes: 2

Related Questions