12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- def run_all_samples(self, account):
- print('Azure Storage Basic Table samples - Starting.')
- table_name = 'tablebasics' + self.random_data.get_random_name(6)
- table_service = None
- try:
- table_service = account.create_table_service()
- # Create a new table
- print('Create a table with name - ' + table_name)
- try:
- table_service.create_table(table_name)
- except Exception as err:
- print('Error creating table, ' + table_name + 'check if it already exists')
-
- # Create a sample entity to insert into the table
- customer = {'PartitionKey': 'Harp', 'RowKey': '1', 'email' : 'harp@contoso.com', 'phone' : '555-555-5555'}
- # Insert the entity into the table
- print('Inserting a new entity into table - ' + table_name)
- table_service.insert_entity(table_name, customer)
- print('Successfully inserted the new entity')
- # Demonstrate how to query the entity
- print('Read the inserted entity.')
- entity = table_service.get_entity(table_name, 'Harp', '1')
- print(entity['email'])
- print(entity['phone'])
- # Demonstrate how to update the entity by changing the phone number
- print('Update an existing entity by changing the phone number')
- customer = {'PartitionKey': 'Harp', 'RowKey': '1', 'email' : 'harp@contoso.com', 'phone' : '425-123-1234'}
- table_service.update_entity(table_name, customer)
- # Demonstrate how to query the updated entity, filter the results with a filter query and select only the value in the phone column
- print('Read the updated entity with a filter query')
- entities = table_service.query_entities(table_name, filter="PartitionKey eq 'Harp'", select='phone')
- for entity in entities:
- print(entity['phone'])
- # Demonstrate how to delete an entity
- print('Delete the entity')
- table_service.delete_entity(table_name, 'Harp', '1')
- print('Successfully deleted the entity')
- except Exception as e:
- if (config.IS_EMULATED):
- print('Error occurred in the sample. If you are using the emulator, please make sure the emulator is running.', e)
- else:
- print('Error occurred in the sample. Please make sure the account name and key are correct.', e)
- finally:
- # Demonstrate deleting the table, if you don't want to have the table deleted comment the below block of code
- print('Deleting the table.')
- if(table_service.exists(table_name)):
- table_service.delete_table(table_name)
- print('Successfully deleted the table')
- print('\nAzure Storage Basic Table samples - Completed.\n')
|