2525from google .cloud import spanner
2626
2727
28+ # [START spanner_create_database]
2829def create_database (instance_id , database_id ):
2930 """Creates a database and tables for sample data."""
3031 spanner_client = spanner .Client ()
@@ -52,8 +53,10 @@ def create_database(instance_id, database_id):
5253
5354 print ('Created database {} on instance {}' .format (
5455 database_id , instance_id ))
56+ # [END spanner_create_database]
5557
5658
59+ # [START spanner_insert_data]
5760def insert_data (instance_id , database_id ):
5861 """Inserts sample data into the given database.
5962
@@ -86,8 +89,10 @@ def insert_data(instance_id, database_id):
8689 (2 , 3 , u'Terrified' )])
8790
8891 print ('Inserted data.' )
92+ # [END spanner_insert_data]
8993
9094
95+ # [START spanner_query_data]
9196def query_data (instance_id , database_id ):
9297 """Queries sample data from the database using SQL."""
9398 spanner_client = spanner .Client ()
@@ -100,8 +105,10 @@ def query_data(instance_id, database_id):
100105
101106 for row in results :
102107 print (u'SingerId: {}, AlbumId: {}, AlbumTitle: {}' .format (* row ))
108+ # [END spanner_query_data]
103109
104110
111+ # [START spanner_read_data]
105112def read_data (instance_id , database_id ):
106113 """Reads sample data from the database."""
107114 spanner_client = spanner .Client ()
@@ -117,8 +124,10 @@ def read_data(instance_id, database_id):
117124
118125 for row in results :
119126 print (u'SingerId: {}, AlbumId: {}, AlbumTitle: {}' .format (* row ))
127+ # [END spanner_read_data]
120128
121129
130+ # [START spanner_read_stale_data]
122131def read_stale_data (instance_id , database_id ):
123132 """Reads sample data from the database. The data is exactly 15 seconds
124133 stale."""
@@ -138,8 +147,10 @@ def read_stale_data(instance_id, database_id):
138147
139148 for row in results :
140149 print (u'SingerId: {}, AlbumId: {}, AlbumTitle: {}' .format (* row ))
150+ # [END spanner_read_stale_data]
141151
142152
153+ # [START spanner_query_data_with_new_column]
143154def query_data_with_new_column (instance_id , database_id ):
144155 """Queries sample data from the database using SQL.
145156
@@ -160,8 +171,10 @@ def query_data_with_new_column(instance_id, database_id):
160171 for row in results :
161172 print (
162173 u'SingerId: {}, AlbumId: {}, MarketingBudget: {}' .format (* row ))
174+ # [END spanner_query_data_with_new_column]
163175
164176
177+ # [START spanner_create_index]
165178def add_index (instance_id , database_id ):
166179 """Adds a simple index to the example database."""
167180 spanner_client = spanner .Client ()
@@ -175,8 +188,10 @@ def add_index(instance_id, database_id):
175188 operation .result ()
176189
177190 print ('Added the AlbumsByAlbumTitle index.' )
191+ # [END spanner_create_index]
178192
179193
194+ # [START spanner_query_data_with_index]
180195def query_data_with_index (
181196 instance_id , database_id , start_title = 'Aardvark' , end_title = 'Goo' ):
182197 """Queries sample data from the database using SQL and an index.
@@ -220,8 +235,10 @@ def query_data_with_index(
220235 print (
221236 u'AlbumId: {}, AlbumTitle: {}, '
222237 'MarketingBudget: {}' .format (* row ))
238+ # [END spanner_query_data_with_index]
223239
224240
241+ # [START spanner_read_data_with_index]
225242def read_data_with_index (instance_id , database_id ):
226243 """Reads sample data from the database using an index.
227244
@@ -246,8 +263,10 @@ def read_data_with_index(instance_id, database_id):
246263
247264 for row in results :
248265 print ('AlbumId: {}, AlbumTitle: {}' .format (* row ))
266+ # [END spanner_read_data_with_index]
249267
250268
269+ # [START spanner_create_storing_index]
251270def add_storing_index (instance_id , database_id ):
252271 """Adds an storing index to the example database."""
253272 spanner_client = spanner .Client ()
@@ -262,8 +281,10 @@ def add_storing_index(instance_id, database_id):
262281 operation .result ()
263282
264283 print ('Added the AlbumsByAlbumTitle2 index.' )
284+ # [END spanner_create_storing_index]
265285
266286
287+ # [START spanner_read_data_with_storing_index]
267288def read_data_with_storing_index (instance_id , database_id ):
268289 """Reads sample data from the database using an index with a storing
269290 clause.
@@ -292,8 +313,10 @@ def read_data_with_storing_index(instance_id, database_id):
292313 print (
293314 u'AlbumId: {}, AlbumTitle: {}, '
294315 'MarketingBudget: {}' .format (* row ))
316+ # [END spanner_read_data_with_storing_index]
295317
296318
319+ # [START spanner_add_column]
297320def add_column (instance_id , database_id ):
298321 """Adds a new column to the Albums table in the example database."""
299322 spanner_client = spanner .Client ()
@@ -307,8 +330,10 @@ def add_column(instance_id, database_id):
307330 operation .result ()
308331
309332 print ('Added the MarketingBudget column.' )
333+ # [END spanner_add_column]
310334
311335
336+ # [START spanner_update_data]
312337def update_data (instance_id , database_id ):
313338 """Updates sample data in the database.
314339
@@ -333,8 +358,10 @@ def update_data(instance_id, database_id):
333358 (2 , 2 , 500000 )])
334359
335360 print ('Updated data.' )
361+ # [END spanner_update_data]
336362
337363
364+ # [START spanner_read_write_transaction]
338365def read_write_transaction (instance_id , database_id ):
339366 """Performs a read-write transaction to update two sample records in the
340367 database.
@@ -395,8 +422,10 @@ def update_albums(transaction):
395422 database .run_in_transaction (update_albums )
396423
397424 print ('Transaction complete.' )
425+ # [END spanner_read_write_transaction]
398426
399427
428+ # [START spanner_read_only_transaction]
400429def read_only_transaction (instance_id , database_id ):
401430 """Reads data inside of a read-only transaction.
402431
@@ -428,6 +457,7 @@ def read_only_transaction(instance_id, database_id):
428457 print ('Results from second read:' )
429458 for row in results :
430459 print (u'SingerId: {}, AlbumId: {}, AlbumTitle: {}' .format (* row ))
460+ # [END spanner_read_only_transaction]
431461
432462
433463if __name__ == '__main__' :
0 commit comments