Skip to main content

snippets_py

Integration

New integration from credential
postgres_pool = yepcode.integration.postgres('credential-slug')
New integration from plain authentication data
from psycopg2 import pool

postgres_pool = pool.SimpleConnectionPool(
minconn=1,
maxconn=10,
host="localhost"",
database="database",
user="username",
password="password"",
port="5432",
sslmode="require", # or "disable"
)

Connect

Connect
connection = postgres_pool.getconn()
cursor = connection.cursor()

Disconnect

Disconnect
cursor.close()
connection.close()

SELECT Text Only

SELECT Text only
query = "SELECT * FROM table_name"

cursor.execute(query)

for row in cursor:
print(row)

SELECT Parameterized

SELECT Parameterized
query = "SELECT * FROM table_name WHERE name = %s"
value = ("some name")

cursor.execute(query, value)

for row in cursor:
print(row)

SELECT Query Object

SELECT Query object
query = "SELECT * FROM table_name WHERE name =  %(name)s"
value = {"name": "some name"}

cursor.execute(query)

for row in cursor:
print(row)

INSERT Text Only

INSERT Text only
query = "INSERT INTO table_name (id, name) VALUES ('some-id', 'some-name')"

cursor.execute(query)
connection.commit()

INSERT Parameterized

INSERT Parameterized
query = "INSERT INTO table_name (id, name) VALUES (%s, %s)"
value = ('some-id','some-name')

cursor.execute(query, value)
connection.commit()

INSERT Query Object

INSERT Query object
query = "INSERT INTO table_name (id, name) VALUES (%(id)s, %(name)s)"
value = {"id":'some-id', "name":'some-name'}

cursor.execute(query, value)
connection.commit()