table_basic_samples_2.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. def run_all_samples(self, account):
  2. print('Azure Storage Basic Table samples - Starting.')
  3. table_name = 'tablebasics' + self.random_data.get_random_name(6)
  4. table_service = None
  5. try:
  6. table_service = account.create_table_service()
  7. # Create a new table
  8. print('Create a table with name - ' + table_name)
  9. try:
  10. table_service.create_table(table_name)
  11. except Exception as err:
  12. print('Error creating table, ' + table_name + 'check if it already exists')
  13. # Create a sample entity to insert into the table
  14. customer = {'PartitionKey': 'Harp', 'RowKey': '1', 'email' : 'harp@contoso.com', 'phone' : '555-555-5555'}
  15. # Insert the entity into the table
  16. print('Inserting a new entity into table - ' + table_name)
  17. table_service.insert_entity(table_name, customer)
  18. print('Successfully inserted the new entity')
  19. # Demonstrate how to query the entity
  20. print('Read the inserted entity.')
  21. entity = table_service.get_entity(table_name, 'Harp', '1')
  22. print(entity['email'])
  23. print(entity['phone'])
  24. # Demonstrate how to update the entity by changing the phone number
  25. print('Update an existing entity by changing the phone number')
  26. customer = {'PartitionKey': 'Harp', 'RowKey': '1', 'email' : 'harp@contoso.com', 'phone' : '425-123-1234'}
  27. table_service.update_entity(table_name, customer)
  28. # Demonstrate how to query the updated entity, filter the results with a filter query and select only the value in the phone column
  29. print('Read the updated entity with a filter query')
  30. entities = table_service.query_entities(table_name, filter="PartitionKey eq 'Harp'", select='phone')
  31. for entity in entities:
  32. print(entity['phone'])
  33. # Demonstrate how to delete an entity
  34. print('Delete the entity')
  35. table_service.delete_entity(table_name, 'Harp', '1')
  36. print('Successfully deleted the entity')
  37. except Exception as e:
  38. if (config.IS_EMULATED):
  39. print('Error occurred in the sample. If you are using the emulator, please make sure the emulator is running.', e)
  40. else:
  41. print('Error occurred in the sample. Please make sure the account name and key are correct.', e)
  42. finally:
  43. # Demonstrate deleting the table, if you don't want to have the table deleted comment the below block of code
  44. print('Deleting the table.')
  45. if(table_service.exists(table_name)):
  46. table_service.delete_table(table_name)
  47. print('Successfully deleted the table')
  48. print('\nAzure Storage Basic Table samples - Completed.\n')