insert into psql code example
Example 1: return insert results in POSTGRESQL
INSERT INTO users (firstname, lastname) VALUES ('Joe', 'Cool') RETURNING id, firstname;
INSERT INTO users (firstname, lastname) VALUES ('Joe', 'Cool') RETURNING *;
Example 2: insert postgres
INSERT INTO films (code, title, did, date_prod, kind)
VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');
Example 3: posgres insert
INSERT INTO films
(code, title, did, date_prod, kind)
VALUES
('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');
Example 4: insert record into table postgresql
try:
connection = psycopg2.connect(**postgres_credentials())
cursor = connection.cursor()
order_types = [(1,'SELL'),(2,'BUY')]
placeholders = ','.join(['%s']*len(order_types))
sql = f"""
INSERT INTO collection.instruction(id, side)
VALUES {placeholders}
"""
insert_statement = cursor.mogrify(sql, order_types)
print(insert_statement.decode('utf-8'))
cursor.execute(insert_statement)
connection.commit()
print('DB entries committed.')
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if connection:
cursor.close()
connection.close()