Skip to content

Commit 0116f6e

Browse files
docs: update SQLAlchemy to commit as you go (#686)
1 parent 2ca6b11 commit 0116f6e

4 files changed

+42
-5
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ The `Connector` itself creates connection objects by calling its `connect` metho
114114
In the Connector's `connect` method below, input your connection string as the first positional argument and the name of the database driver for the second positional argument. Insert the rest of your connection keyword arguments like user, password and database. You can also set the optional `timeout` or `ip_type` keyword arguments.
115115

116116
To use this connector with SQLAlchemy, use the `creator` argument for `sqlalchemy.create_engine`:
117+
117118
```python
118119
from google.cloud.sql.connector import Connector
119120
import sqlalchemy
@@ -140,6 +141,7 @@ pool = sqlalchemy.create_engine(
140141
```
141142

142143
The returned connection pool engine can then be used to query and modify the database.
144+
143145
```python
144146
# insert statement
145147
insert_stmt = sqlalchemy.text(
@@ -153,6 +155,9 @@ with pool.connect() as db_conn:
153155
# query database
154156
result = db_conn.execute(sqlalchemy.text("SELECT * from my_table")).fetchall()
155157

158+
# commit transaction (SQLAlchemy v2.X.X is commit as you go)
159+
db_conn.commit()
160+
156161
# Do something with the results
157162
for row in result:
158163
print(row)
@@ -226,6 +231,9 @@ with pool.connect() as db_conn:
226231
# insert into database
227232
db_conn.execute(insert_stmt, parameters={"id": "book1", "title": "Book One"})
228233

234+
# commit transaction (SQLAlchemy v2.X.X is commit as you go)
235+
db_conn.commit()
236+
229237
# query database
230238
result = db_conn.execute(sqlalchemy.text("SELECT * from my_table")).fetchall()
231239

@@ -235,8 +243,10 @@ with pool.connect() as db_conn:
235243
```
236244

237245
### Specifying Public or Private IP
246+
238247
The Cloud SQL Connector for Python can be used to connect to Cloud SQL instances using both public and private IP addresses. To specify which IP address to use to connect, set the `ip_type` keyword argument Possible values are `IPTypes.PUBLIC` and `IPTypes.PRIVATE`.
239248
Example:
249+
240250
```python
241251
from google.cloud.sql.connector import IPTypes
242252

@@ -251,6 +261,7 @@ connector.connect(
251261
Note: If specifying Private IP, your application must already be in the same VPC network as your Cloud SQL Instance.
252262

253263
### IAM Authentication
264+
254265
Connections using [Automatic IAM database authentication](https://cloud.google.com/sql/docs/postgres/authentication#automatic) are supported when using Postgres or MySQL drivers.
255266
First, make sure to [configure your Cloud SQL Instance to allow IAM authentication](https://cloud.google.com/sql/docs/postgres/create-edit-iam-instances#configure-iam-db-instance)
256267
and [add an IAM database user](https://cloud.google.com/sql/docs/postgres/create-manage-iam-users#creating-a-database-user).
@@ -262,6 +273,7 @@ In the call to connect, set the `enable_iam_auth` keyword argument to true and t
262273
> MySQL: For an IAM user account, this is the user's email address, without the @ or domain name. For example, for `[email protected]`, set the `user` argument to `test-user`. For a service account, this is the service account's email address without the `@project-id.iam.gserviceaccount.com` suffix.
263274
264275
Example:
276+
265277
```python
266278
connector.connect(
267279
"project:region:instance",
@@ -273,9 +285,11 @@ connector.connect(
273285
```
274286

275287
### SQL Server Active Directory Authentication
288+
276289
Active Directory authentication for SQL Server instances is currently only supported on Windows. First, make sure to follow [these steps](https://cloud.google.com/blog/topics/developers-practitioners/creating-sql-server-instance-integrated-active-directory-using-google-cloud-sql) to set up a Managed AD domain and join your Cloud SQL instance to the domain. [See here for more info on Cloud SQL Active Directory integration](https://cloud.google.com/sql/docs/sqlserver/ad).
277290

278291
Once you have followed the steps linked above, you can run the following code to return a connection object:
292+
279293
```python
280294
connector.connect(
281295
"project:region:instance",
@@ -285,7 +299,9 @@ connector.connect(
285299
server_name="public.[instance].[location].[project].cloudsql.[domain]",
286300
)
287301
```
302+
288303
Or, if using Private IP:
304+
289305
```python
290306
connector.connect(
291307
"project:region:instance",
@@ -298,11 +314,13 @@ connector.connect(
298314
```
299315

300316
### Using the Python Connector with Python Web Frameworks
317+
301318
The Python Connector can be used alongside popular Python web frameworks such
302319
as Flask, FastAPI, etc, to integrate Cloud SQL databases within your
303320
web applications.
304321

305322
#### Flask-SQLAlchemy
323+
306324
[Flask-SQLAlchemy](https://flask-sqlalchemy.palletsprojects.com/en/2.x/)
307325
is an extension for [Flask](https://flask.palletsprojects.com/en/2.2.x/)
308326
that adds support for [SQLAlchemy](https://www.sqlalchemy.org/) to your
@@ -348,6 +366,7 @@ For more details on how to use Flask-SQLAlchemy, check out the
348366
[Flask-SQLAlchemy Quickstarts](https://flask-sqlalchemy.palletsprojects.com/en/2.x/quickstart/#)
349367

350368
#### FastAPI
369+
351370
[FastAPI](https://fastapi.tiangolo.com/) is a modern, fast (high-performance),
352371
web framework for building APIs with Python based on standard Python type hints.
353372

@@ -390,6 +409,7 @@ To learn more about integrating a database into your FastAPI application,
390409
follow along the [FastAPI SQL Database guide](https://fastapi.tiangolo.com/tutorial/sql-databases/#create-the-database-models).
391410

392411
### Async Driver Usage
412+
393413
The Cloud SQL Connector is compatible with
394414
[asyncio](https://docs.python.org/3/library/asyncio.html) to improve the speed
395415
and efficiency of database connections through concurrency. You can use all

samples/notebooks/mysql_python_connector.ipynb

+9-2
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@
403403
"source": [
404404
"# install dependencies\n",
405405
"import sys\n",
406-
"!{sys.executable} -m pip install cloud-sql-python-connector[\"pymysql\"] SQLAlchemy==2.0.2"
406+
"!{sys.executable} -m pip install cloud-sql-python-connector[\"pymysql\"] SQLAlchemy==2.0.7"
407407
],
408408
"execution_count": null,
409409
"outputs": []
@@ -524,6 +524,10 @@
524524
" \"PRIMARY KEY (id));\"\n",
525525
" )\n",
526526
" )\n",
527+
"\n",
528+
" # commit transaction (SQLAlchemy v2.X.X is commit as you go)\n",
529+
" db_conn.commit()\n",
530+
"\n",
527531
" # insert data into our ratings table\n",
528532
" insert_stmt = sqlalchemy.text(\n",
529533
" \"INSERT INTO ratings (name, origin, rating) VALUES (:name, :origin, :rating)\",\n",
@@ -534,6 +538,9 @@
534538
" db_conn.execute(insert_stmt, parameters={\"name\": \"BÀNH MÌ\", \"origin\": \"Vietnam\", \"rating\": 9.1})\n",
535539
" db_conn.execute(insert_stmt, parameters={\"name\": \"CROQUE MADAME\", \"origin\": \"France\", \"rating\": 8.3})\n",
536540
"\n",
541+
" # commit transactions\n",
542+
" db_conn.commit()\n",
543+
"\n",
537544
" # query and fetch ratings table\n",
538545
" results = db_conn.execute(sqlalchemy.text(\"SELECT * FROM ratings\")).fetchall()\n",
539546
"\n",
@@ -809,4 +816,4 @@
809816
"outputs": []
810817
}
811818
]
812-
}
819+
}

samples/notebooks/postgres_python_connector.ipynb

+9-2
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@
403403
"source": [
404404
"# install dependencies\n",
405405
"import sys\n",
406-
"!{sys.executable} -m pip install cloud-sql-python-connector[\"pg8000\"] SQLAlchemy==2.0.2"
406+
"!{sys.executable} -m pip install cloud-sql-python-connector[\"pg8000\"] SQLAlchemy==2.0.7"
407407
],
408408
"execution_count": null,
409409
"outputs": []
@@ -524,6 +524,10 @@
524524
" \"PRIMARY KEY (id));\"\n",
525525
" )\n",
526526
" )\n",
527+
"\n",
528+
" # commit transaction (SQLAlchemy v2.X.X is commit as you go)\n",
529+
" db_conn.commit()\n",
530+
"\n",
527531
" # insert data into our ratings table\n",
528532
" insert_stmt = sqlalchemy.text(\n",
529533
" \"INSERT INTO ratings (name, origin, rating) VALUES (:name, :origin, :rating)\",\n",
@@ -534,6 +538,9 @@
534538
" db_conn.execute(insert_stmt, parameters={\"name\": \"BÀNH MÌ\", \"origin\": \"Vietnam\", \"rating\": 9.1})\n",
535539
" db_conn.execute(insert_stmt, parameters={\"name\": \"CROQUE MADAME\", \"origin\": \"France\", \"rating\": 8.3})\n",
536540
"\n",
541+
" # commit transactions\n",
542+
" db_conn.commit()\n",
543+
"\n",
537544
" # query and fetch ratings table\n",
538545
" results = db_conn.execute(sqlalchemy.text(\"SELECT * FROM ratings\")).fetchall()\n",
539546
"\n",
@@ -805,4 +812,4 @@
805812
"outputs": []
806813
}
807814
]
808-
}
815+
}

samples/notebooks/sqlserver_python_connector.ipynb

+4-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@
380380
"source": [
381381
"# install dependencies\n",
382382
"import sys\n",
383-
"!{sys.executable} -m pip install cloud-sql-python-connector[\"pytds\"] SQLAlchemy==2.0.4 sqlalchemy-pytds==0.3.5\n"
383+
"!{sys.executable} -m pip install cloud-sql-python-connector[\"pytds\"] SQLAlchemy==2.0.7 sqlalchemy-pytds==0.3.5\n"
384384
],
385385
"execution_count": null,
386386
"outputs": []
@@ -522,6 +522,9 @@
522522
" db_conn.execute(insert_stmt, parameters={\"name\": \"BÀNH MÌ\", \"origin\": \"Vietnam\", \"rating\": 9.1})\n",
523523
" db_conn.execute(insert_stmt, parameters={\"name\": \"CROQUE MADAME\", \"origin\": \"France\", \"rating\": 8.3})\n",
524524
"\n",
525+
" # commit transaction (SQLAlchemy v2.X.X is commit as you go)\n",
526+
" db_conn.commit()\n",
527+
"\n",
525528
" # query and fetch ratings table\n",
526529
" results = db_conn.execute(sqlalchemy.text(\"SELECT * FROM ratings\")).fetchall()\n",
527530
"\n",

0 commit comments

Comments
 (0)