@@ -701,7 +701,7 @@ def query_with_array_of_struct(instance_id, database_id):
701
701
param_types = {'names' : param_types .Array (name_type )})
702
702
703
703
for row in results :
704
- print (u'SingerId: {}' .format (* row ))
704
+ print (u'SingerId: {}' .format (* row ))
705
705
# [END spanner_query_data_with_array_of_struct]
706
706
707
707
@@ -725,7 +725,7 @@ def query_struct_field(instance_id, database_id):
725
725
param_types = {'name' : name_type })
726
726
727
727
for row in results :
728
- print (u'SingerId: {}' .format (* row ))
728
+ print (u'SingerId: {}' .format (* row ))
729
729
# [START spanner_field_access_on_struct_parameters]
730
730
731
731
@@ -768,7 +768,7 @@ def query_nested_struct_field(instance_id, database_id):
768
768
)
769
769
770
770
for row in results :
771
- print (u'SingerId: {} SongName: {}' .format (* row ))
771
+ print (u'SingerId: {} SongName: {}' .format (* row ))
772
772
# [END spanner_field_access_on_nested_struct_parameters]
773
773
774
774
@@ -998,7 +998,7 @@ def transfer_budget(transaction):
998
998
)
999
999
1000
1000
print ("Transferred {} from Album1's budget to Album2's" .format (
1001
- transfer_amount ))
1001
+ transfer_amount ))
1002
1002
1003
1003
database .run_in_transaction (transfer_budget )
1004
1004
# [END spanner_dml_getting_started_update]
@@ -1039,6 +1039,41 @@ def delete_data_with_partitioned_dml(instance_id, database_id):
1039
1039
# [END spanner_dml_partitioned_delete]
1040
1040
1041
1041
1042
+ def update_with_batch_dml (instance_id , database_id ):
1043
+ """Updates sample data in the database using Batch DML. """
1044
+ # [START spanner_dml_batch_update]
1045
+ # instance_id = "your-spanner-instance"
1046
+ # database_id = "your-spanner-db-id"
1047
+
1048
+ spanner_client = spanner .Client ()
1049
+ instance = spanner_client .instance (instance_id )
1050
+ database = instance .database (database_id )
1051
+
1052
+ insert_statement = (
1053
+ "INSERT INTO Albums "
1054
+ "(SingerId, AlbumId, AlbumTitle, MarketingBudget) "
1055
+ "VALUES (1, 3, 'Test Album Title', 10000)"
1056
+ )
1057
+
1058
+ update_statement = (
1059
+ "UPDATE Albums "
1060
+ "SET MarketingBudget = MarketingBudget * 2 "
1061
+ "WHERE SingerId = 1 and AlbumId = 3"
1062
+ )
1063
+
1064
+ def update_albums (transaction ):
1065
+ row_cts = transaction .batch_update ([
1066
+ insert_statement ,
1067
+ update_statement ,
1068
+ ])
1069
+
1070
+ print ("Executed {} SQL statements using Batch DML." .format (
1071
+ len (row_cts )))
1072
+
1073
+ database .run_in_transaction (update_albums )
1074
+ # [END spanner_dml_batch_update]
1075
+
1076
+
1042
1077
if __name__ == '__main__' : # noqa: C901
1043
1078
parser = argparse .ArgumentParser (
1044
1079
description = __doc__ ,
@@ -1118,6 +1153,9 @@ def delete_data_with_partitioned_dml(instance_id, database_id):
1118
1153
subparsers .add_parser (
1119
1154
'delete_data_with_partitioned_dml' ,
1120
1155
help = delete_data_with_partitioned_dml .__doc__ )
1156
+ subparsers .add_parser (
1157
+ 'update_with_batch_dml' ,
1158
+ help = update_with_batch_dml .__doc__ )
1121
1159
1122
1160
args = parser .parse_args ()
1123
1161
@@ -1195,3 +1233,5 @@ def delete_data_with_partitioned_dml(instance_id, database_id):
1195
1233
update_data_with_partitioned_dml (args .instance_id , args .database_id )
1196
1234
elif args .command == 'delete_data_with_partitioned_dml' :
1197
1235
delete_data_with_partitioned_dml (args .instance_id , args .database_id )
1236
+ elif args .command == 'update_with_batch_dml' :
1237
+ update_with_batch_dml (args .instance_id , args .database_id )
0 commit comments