Skip to content

Commit

Permalink
chore: update context manager sample usage (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon committed Feb 28, 2024
1 parent 2ff8708 commit bb693e6
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,43 +183,49 @@ Connector as a context manager:
from google.cloud.alloydb.connector import Connector
import sqlalchemy

# build connection
def getconn():
with Connector() as connector:
# helper function to return SQLAlchemy connection pool
def init_connection_pool(connector: Connector) -> sqlalchemy.engine.Engine:
# function used to generate database connection
def getconn():
conn = connector.connect(
"projects/<YOUR_PROJECT>/locations/<YOUR_REGION>/clusters/<YOUR_CLUSTER>/instances/<YOUR_INSTANCE>",
"pg8000",
user="my-user",
password="my-password",
db="my-db-name"
)
return conn

# create connection pool
pool = sqlalchemy.create_engine(
"postgresql+pg8000://",
creator=getconn,
)

# insert statement
insert_stmt = sqlalchemy.text(
"INSERT INTO my_table (id, title) VALUES (:id, :title)",
)

# interact with AlloyDB database using connection pool
with pool.connect() as db_conn:
# insert into database
db_conn.execute(insert_stmt, parameters={"id": "book1", "title": "Book One"})

# commit transaction (SQLAlchemy v2.X.X is commit as you go)
db_conn.commit()
return conn

# query database
result = db_conn.execute(sqlalchemy.text("SELECT * from my_table")).fetchall()
# create connection pool
pool = sqlalchemy.create_engine(
"postgresql+pg8000://",
creator=getconn,
)
return pool

# Do something with the results
for row in result:
print(row)
# initialize Connector as context manager
with Connector() as connector:
# initialize connection pool
pool = init_connection_pool(connector)
# insert statement
insert_stmt = sqlalchemy.text(
"INSERT INTO my_table (id, title) VALUES (:id, :title)",
)

# interact with AlloyDB database using connection pool
with pool.connect() as db_conn:
# insert into database
db_conn.execute(insert_stmt, parameters={"id": "book1", "title": "Book One"})

# commit transaction (SQLAlchemy v2.X.X is commit as you go)
db_conn.commit()

# query database
result = db_conn.execute(sqlalchemy.text("SELECT * from my_table")).fetchall()

# Do something with the results
for row in result:
print(row)
```

### Async Driver Usage
Expand Down

0 comments on commit bb693e6

Please sign in to comment.