1
#-------------------------------------------------------------------------
2
# Copyright (c) Microsoft. All rights reserved.
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14
#--------------------------------------------------------------------------
17
How To: Create a Table
18
----------------------
19
>>> from azure.storage import *
20
>>> table_service = TableService(name, key)
21
>>> table_service.create_table('tasktable')
24
How to Add an Entity to a Table
25
-------------------------------
26
>>> task = {'PartitionKey': 'tasksSeattle', 'RowKey': '1', 'description' : 'Take out the trash', 'priority' : 200}
27
>>> entity = table_service.insert_entity('tasktable', task)
30
>>> task.PartitionKey = 'tasksSeattle'
32
>>> task.description = 'Wash the car'
33
>>> task.priority = 100
34
>>> entity = table_service.insert_entity('tasktable', task)
36
How to Update an Entity
37
-----------------------
38
>>> task = {'description' : 'Take out the garbage', 'priority' : 250}
39
>>> entity = table_service.update_entity('tasktable', 'tasksSeattle', '1', task)
41
>>> task = {'description' : 'Take out the garbage again', 'priority' : 250}
42
>>> entity = table_service.insert_or_replace_entity('tasktable', 'tasksSeattle', '1', task)
44
>>> task = {'description' : 'Buy detergent', 'priority' : 300}
45
>>> entity = table_service.insert_or_replace_entity('tasktable', 'tasksSeattle', '3', task)
48
How to Change a Group of Entities
49
---------------------------------
50
>>> task10 = {'PartitionKey': 'tasksSeattle', 'RowKey': '10', 'description' : 'Go grocery shopping', 'priority' : 400}
51
>>> task11 = {'PartitionKey': 'tasksSeattle', 'RowKey': '11', 'description' : 'Clean the bathroom', 'priority' : 100}
52
>>> table_service.begin_batch()
53
>>> table_service.insert_entity('tasktable', task10)
54
>>> table_service.insert_entity('tasktable', task11)
55
>>> table_service.commit_batch()
57
How to Query for an Entity
58
--------------------------
59
>>> task = table_service.get_entity('tasktable', 'tasksSeattle', '1')
60
>>> print(task.description)
61
Take out the garbage again
62
>>> print(task.priority)
65
>>> task = table_service.get_entity('tasktable', 'tasksSeattle', '10')
66
>>> print(task.description)
68
>>> print(task.priority)
71
How to Query a Set of Entities
72
------------------------------
73
>>> tasks = table_service.query_entities('tasktable', "PartitionKey eq 'tasksSeattle'")
74
>>> for task in tasks:
75
... print(task.description)
76
... print(task.priority)
77
Take out the garbage again
88
How to Query a Subset of Entity Properties
89
------------------------------------------
90
>>> tasks = table_service.query_entities('tasktable', "PartitionKey eq 'tasksSeattle'", 'description')
91
>>> for task in tasks:
92
... print(task.description)
93
Take out the garbage again
99
How to Delete an Entity
100
-----------------------
101
>>> table_service.delete_entity('tasktable', 'tasksSeattle', '1')
103
How to Delete a Table
104
---------------------
105
>>> table_service.delete_table('tasktable')
109
from util import credentials
111
name = credentials.getStorageServicesName()
112
key = credentials.getStorageServicesKey()
114
if __name__ == "__main__":