Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cloud Spanner DML Insert and Update examples to README #8698

Merged
merged 2 commits into from Jul 30, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 45 additions & 2 deletions spanner/README.rst
Expand Up @@ -159,7 +159,29 @@ Once you have a transaction object (such as the first argument sent to
print(row)


Insert records using a Transaction
Insert records using Data Manipulation Language (DML) with a Transaction
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tseaver marked this conversation as resolved.
Show resolved Hide resolved

Use the ``execute_update()`` method to execute a DML statement:

.. code:: python

spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)

def insert_singers(transaction):
row_ct = transaction.execute_update(
"INSERT Singers (SingerId, FirstName, LastName) "
" VALUES (10, 'Virginia', 'Watson')"
)

print("{} record(s) inserted.".format(row_ct))

database.run_in_transaction(insert_singers)


Insert records using Mutations with a Transaction
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tseaver marked this conversation as resolved.
Show resolved Hide resolved

To add one or more records to a table, use ``insert``:
Expand All @@ -176,7 +198,28 @@ To add one or more records to a table, use ``insert``:
)


Update records using a Transaction
Update records using Data Manipulation Language (DML) with a Transaction
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tseaver marked this conversation as resolved.
Show resolved Hide resolved

.. code:: python

spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)

def update_albums(transaction):
row_ct = transaction.execute_update(
"UPDATE Albums "
"SET MarketingBudget = MarketingBudget * 2 "
"WHERE SingerId = 1 and AlbumId = 1"
)

print("{} record(s) updated.".format(row_ct))

database.run_in_transaction(update_albums)


Update records using Mutations with a Transaction
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tseaver marked this conversation as resolved.
Show resolved Hide resolved

``Transaction.update`` updates one or more existing records in a table. Fails
Expand Down