Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c66eb83

Browse filesBrowse files
authored
Add queryWithParameter to Cloud Spanner sample. (GoogleCloudPlatform#2153)
* Add queryWithParameter to Cloud Spanner sample. * Lint. * Update to fix test.
1 parent 1a2d2d7 commit c66eb83
Copy full SHA for c66eb83

File tree

Expand file treeCollapse file tree

2 files changed

+32
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+32
-1
lines changed

‎spanner/cloud-client/snippets.py

Copy file name to clipboardExpand all lines: spanner/cloud-client/snippets.py
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,27 @@ def insert_singers(transaction):
943943
# [END spanner_dml_getting_started_insert]
944944

945945

946+
def query_data_with_parameter(instance_id, database_id):
947+
"""Queries sample data from the database using SQL with a parameter."""
948+
# [START spanner_query_with_parameter]
949+
# instance_id = "your-spanner-instance"
950+
# database_id = "your-spanner-db-id"
951+
spanner_client = spanner.Client()
952+
instance = spanner_client.instance(instance_id)
953+
database = instance.database(database_id)
954+
955+
with database.snapshot() as snapshot:
956+
results = snapshot.execute_sql(
957+
"SELECT SingerId, FirstName, LastName FROM Singers "
958+
"WHERE LastName = @lastName",
959+
params={"lastName": "Garcia"},
960+
param_types={"lastName": spanner.param_types.STRING})
961+
962+
for row in results:
963+
print(u"SingerId: {}, FirstName: {}, LastName: {}".format(*row))
964+
# [END spanner_query_with_parameter]
965+
966+
946967
def write_with_dml_transaction(instance_id, database_id):
947968
""" Transfers a marketing budget from one album to another. """
948969
# [START spanner_dml_getting_started_update]
@@ -1145,6 +1166,8 @@ def update_albums(transaction):
11451166
'update_data_with_dml_struct',
11461167
help=update_data_with_dml_struct.__doc__)
11471168
subparsers.add_parser('insert_with_dml', help=insert_with_dml.__doc__)
1169+
subparsers.add_parser(
1170+
'query_data_with_parameter', help=query_data_with_parameter.__doc__)
11481171
subparsers.add_parser(
11491172
'write_with_dml_transaction', help=write_with_dml_transaction.__doc__)
11501173
subparsers.add_parser(
@@ -1227,6 +1250,8 @@ def update_albums(transaction):
12271250
update_data_with_dml_struct(args.instance_id, args.database_id)
12281251
elif args.command == 'insert_with_dml':
12291252
insert_with_dml(args.instance_id, args.database_id)
1253+
elif args.command == 'query_data_with_parameter':
1254+
query_data_with_parameter(args.instance_id, args.database_id)
12301255
elif args.command == 'write_with_dml_transaction':
12311256
write_with_dml_transaction(args.instance_id, args.database_id)
12321257
elif args.command == 'update_data_with_partitioned_dml':

‎spanner/cloud-client/snippets_test.py

Copy file name to clipboardExpand all lines: spanner/cloud-client/snippets_test.py
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def test_query_with_struct(capsys):
202202
def test_query_with_array_of_struct(capsys):
203203
snippets.query_with_array_of_struct(INSTANCE_ID, DATABASE_ID)
204204
out, _ = capsys.readouterr()
205-
assert 'SingerId: 6\nSingerId: 7' in out
205+
assert 'SingerId: 8\nSingerId: 7\nSingerId: 6' in out
206206

207207

208208
def test_query_struct_field(capsys):
@@ -261,6 +261,12 @@ def test_insert_with_dml(capsys):
261261
assert '4 record(s) inserted' in out
262262

263263

264+
def test_query_data_with_parameter(capsys):
265+
snippets.query_data_with_parameter(INSTANCE_ID, DATABASE_ID)
266+
out, _ = capsys.readouterr()
267+
assert 'SingerId: 12, FirstName: Melissa, LastName: Garcia' in out
268+
269+
264270
def test_write_with_dml_transaction(capsys):
265271
snippets.write_with_dml_transaction(INSTANCE_ID, DATABASE_ID)
266272
out, _ = capsys.readouterr()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.