diff --git a/spanner/cloud-client/snippets.py b/spanner/cloud-client/snippets.py index 9bbe0fbd8c21..8d4019416055 100644 --- a/spanner/cloud-client/snippets.py +++ b/spanner/cloud-client/snippets.py @@ -943,6 +943,27 @@ def insert_singers(transaction): # [END spanner_dml_getting_started_insert] +def query_data_with_parameter(instance_id, database_id): + """Queries sample data from the database using SQL with a parameter.""" + # [START spanner_query_with_parameter] + # instance_id = "your-spanner-instance" + # database_id = "your-spanner-db-id" + spanner_client = spanner.Client() + instance = spanner_client.instance(instance_id) + database = instance.database(database_id) + + with database.snapshot() as snapshot: + results = snapshot.execute_sql( + "SELECT SingerId, FirstName, LastName FROM Singers " + "WHERE LastName = @lastName", + params={"lastName": "Garcia"}, + param_types={"lastName": spanner.param_types.STRING}) + + for row in results: + print(u"SingerId: {}, FirstName: {}, LastName: {}".format(*row)) +# [END spanner_query_with_parameter] + + def write_with_dml_transaction(instance_id, database_id): """ Transfers a marketing budget from one album to another. """ # [START spanner_dml_getting_started_update] @@ -1145,6 +1166,8 @@ def update_albums(transaction): 'update_data_with_dml_struct', help=update_data_with_dml_struct.__doc__) subparsers.add_parser('insert_with_dml', help=insert_with_dml.__doc__) + subparsers.add_parser( + 'query_data_with_parameter', help=query_data_with_parameter.__doc__) subparsers.add_parser( 'write_with_dml_transaction', help=write_with_dml_transaction.__doc__) subparsers.add_parser( @@ -1227,6 +1250,8 @@ def update_albums(transaction): update_data_with_dml_struct(args.instance_id, args.database_id) elif args.command == 'insert_with_dml': insert_with_dml(args.instance_id, args.database_id) + elif args.command == 'query_data_with_parameter': + query_data_with_parameter(args.instance_id, args.database_id) elif args.command == 'write_with_dml_transaction': write_with_dml_transaction(args.instance_id, args.database_id) elif args.command == 'update_data_with_partitioned_dml': diff --git a/spanner/cloud-client/snippets_test.py b/spanner/cloud-client/snippets_test.py index 9bd39d27a571..199d69f16dbf 100644 --- a/spanner/cloud-client/snippets_test.py +++ b/spanner/cloud-client/snippets_test.py @@ -202,7 +202,7 @@ def test_query_with_struct(capsys): def test_query_with_array_of_struct(capsys): snippets.query_with_array_of_struct(INSTANCE_ID, DATABASE_ID) out, _ = capsys.readouterr() - assert 'SingerId: 6\nSingerId: 7' in out + assert 'SingerId: 8\nSingerId: 7\nSingerId: 6' in out def test_query_struct_field(capsys): @@ -261,6 +261,12 @@ def test_insert_with_dml(capsys): assert '4 record(s) inserted' in out +def test_query_data_with_parameter(capsys): + snippets.query_data_with_parameter(INSTANCE_ID, DATABASE_ID) + out, _ = capsys.readouterr() + assert 'SingerId: 12, FirstName: Melissa, LastName: Garcia' in out + + def test_write_with_dml_transaction(capsys): snippets.write_with_dml_transaction(INSTANCE_ID, DATABASE_ID) out, _ = capsys.readouterr()