Apps Script can connect to external databases through the JDBC service , a wrapper around the standard Java Database Connectivity technology . The JDBC service supports Google Cloud SQL for MySQL , MySQL, Microsoft SQL Server, and Oracle databases.
To update an external database with JDBC, your script must open a connection to the database and then make changes by sending SQL statements.
Google Cloud SQL databases
Google Cloud SQL lets you create relational databases that live in Google's cloud. Note that Cloud SQL might incur charges based on your usage.
You can create a Google Cloud SQL instance by following the steps listed in the Cloud SQL quickstart .
Creating Google Cloud SQL connections
There are two ways of establishing a connection with a Google Cloud SQL database using Apps Script's JDBC service :
- (Recommended) Connecting using Jdbc.getCloudSqlConnection(url)
- Connecting using Jdbc.getConnection(url)
These methods are explained below. Both are valid, but the second method requires you to authorize a set of IP ranges for access to your database.
Using Jdbc.getCloudSqlConnection(url) (recommended)
This method creates a connection to a Google Cloud SQL MySQL instance using the Jdbc.getCloudSqlConnection(url)
method. The database URL has the form of jdbc:google:mysql://subname
, where subname
is the MySQL Instance connection namelisted on the Cloud SQL instance Overviewpage in the Google Cloud console
.
To connect to Cloud SQL SQL Server, see Jdbc.getConnection(url) .
Using Jdbc.getConnection(url)
In order to use this method, you must authorize certain Classless Inter-Domain Routing (CIDR) IP address ranges so that Apps Script's servers can connect to your database. Before running your script, complete the following steps:
-
In your Google Cloud SQL instance, authorize the IP ranges , one at at time from this data source .
-
Copy the URL that was assigned to your database; it should have the form
jdbc:mysql:subname
.
Once you've authorized these IP ranges, you can create connections to your Google Cloud SQL instance using one of the Jdbc.getConnection(url) methods and the URL you copied above.
Other databases
If you already have your own MySQL, Microsoft SQL Server, or Oracle database, you can connect to it through Apps Script's JDBC service.
Creating other database connections
In order to create a database connection using the Apps Script JDBC service , in your database settings you must authorize IP ranges from this data source .
Once these allowlists are in place, you can create a connection to the database using one of the Jdbc.getConnection(url) methods and your database's URL.
Sample code
The sample code below assumes you are connecting to a Google Cloud SQL database, and creates database connections using the Jdbc.getCloudSqlConnection(url) method. For other databases you must use the Jdbc.getConnection(url) method to create database connections.
For more information on the JDBC methods, see the Java documentation for JDBC .
Create a database, user, and table
Most developers use the MySQL command-line tool
to
create databases, users, and tables. However, it's possible to do the same
thing in Apps Script, as shown below. It's a good idea to create at least one
other user so that your script doesn't always have to connect to the database as root
.
Write to the database
The examples below demonstrate how to write a single record to the database as well as a batch of 500 records. Batching is vital for bulk operations.
Note also the use of parameterized statements, in which the variables are
denoted by ?
. To prevent SQL injection attacks
, you should use
parameterized statements to escape all user-supplied data.
Read from the database
This example demonstrates how to read a large number of records from the database, looping over the result set as necessary.
Closing connections
JDBC connections close automatically when a script finishes executing. (Keep in
mind that a single google.script.run
call counts as a complete execution, even if the HTML service page that made the
call remains open.)
Nonetheless, if you know you're done with a connection, statement, or result set
before the end of the script, it's a good idea to close them manually by calling JdbcConnection.close()
, JdbcStatement.close()
,
or JdbcResultSet.close()
.
Showing an alert or prompt dialog also terminates any open JDBC connections. However, other showing UI elements—like custom menus or dialogs and sidebars with custom content—does not.
Google, Google Workspace, and related marks and logos are trademarks of Google LLC. All other company and product names are trademarks of the companies with which they are associated.