You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To use the connector: import the connector by including the following statement at the top of your Python file:
48
+
To use the connector: import the connector and SQLAlchemy by including the following statements at the top of your Python file:
49
49
```Python
50
50
from google.cloud.sql.connector import connector
51
+
import sqlalchemy
51
52
```
52
53
53
-
Use the connector to create a connection object by calling the connect method. 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.
54
+
The connector itself creates connection objects by calling its `connect` method but does not manage database connection pooling. For this reason, it is recommended to use the connector alongside a library that can create connection pools, such as [SQLAlchemy](https://www.sqlalchemy.org/). This will allow for connections to remain open and be reused, reducing connection overhead and the number of connections needed.
54
55
55
-
```
56
-
conn = connector.connect(
57
-
"project:region:instance",
58
-
"pymysql",
59
-
user="root",
60
-
password="shhh",
61
-
db="your-db-name"
62
-
... insert other kwargs ...
63
-
)
64
-
```
65
-
66
-
The returned DB-API 2.0 compliant connection object can then be used to query and modify the database:
67
-
```
68
-
# Execute a query
69
-
cursor = conn.cursor()
70
-
cursor.execute("SELECT * from my_table")
71
-
72
-
# Fetch the results
73
-
result = cursor.fetchall()
74
-
75
-
# Do something with the results
76
-
for row in result:
77
-
print(row)
78
-
```
56
+
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.
79
57
80
58
To use this connector with SQLAlchemy, use the `creator` argument for `sqlalchemy.create_engine`:
result = db_conn.execute("SELECT * from my_table").fetchall()
89
+
90
+
# Do something with the results
91
+
for row in result:
92
+
print(row)
93
+
```
94
+
95
+
**Note**: For more examples of using SQLAlchemy to manage connection pooling with the connector, please see [Cloud SQL SQLAlchemy Samples](https://cloud.google.com/sql/docs/postgres/connect-connectors#python_1).
96
+
98
97
**Note for SQL Server users**: If your SQL Server instance requires SSL, you need to download the CA certificate for your instance and include `cafile={path to downloaded certificate}` and `validate_host=False`. This is a workaround for a [known issue](https://issuetracker.google.com/184867147).
0 commit comments