Skip to content

Commit cd3a05d

Browse files
Fix Spanner README. (#3796)
1 parent c33e9d4 commit cd3a05d

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

spanner/README.rst

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,27 @@ as a callback to ``database.run_in_transaction``:
5757
# The use of @parameters is recommended rather than doing your
5858
# own string interpolation; this provides protections against
5959
# SQL injection attacks.
60-
query = """UPDATE people
61-
SET anniversary = @uxts
60+
query = """SELECT anniversary FROM people
6261
WHERE id = @person_id"""
6362
6463
# When executing the SQL statement, the query and parameters are sent
6564
# as separate arguments. When using parameters, you must specify
6665
# both the parameters themselves and their types.
67-
transaction.execute_sql(
66+
row = transaction.execute_sql(
6867
query=query,
69-
params={'person_id': person_id, 'uxts': unix_timestamp},
68+
params={'person_id': person_id},
7069
param_types={
7170
'person_id': types.INT64_PARAM_TYPE,
72-
'uxts': types.INT64_PARAM_TYPE,
7371
},
72+
).one()
73+
74+
# Now perform an update on the data.
75+
old_anniversary = row[0]
76+
new_anniversary = _compute_anniversary(old_anniversary, years)
77+
transaction.update(
78+
'people',
79+
['person_id', 'anniversary'],
80+
[person_id, new_anniversary],
7481
)
7582
7683
# Actually run the `update_anniversary` function in a transaction.
@@ -140,4 +147,4 @@ Learn More
140147
See the ``google-cloud-python`` API `Cloud Spanner documentation`_ to learn how
141148
to connect to Cloud Spanner using this Client Library.
142149

143-
.. _Cloud Spanner documentation: https://googlecloudplatform.github.io/google-cloud-python/latest/bigquery/usage.html
150+
.. _Cloud Spanner documentation: https://googlecloudplatform.github.io/google-cloud-python/latest/spanner/usage.html

0 commit comments

Comments
 (0)