1
+ {
2
+ "metadata" : {
3
+ "language_info" : {
4
+ "codemirror_mode" : {
5
+ "name" : " ipython" ,
6
+ "version" : 3
7
+ },
8
+ "file_extension" : " .py" ,
9
+ "mimetype" : " text/x-python" ,
10
+ "name" : " python" ,
11
+ "nbconvert_exporter" : " python" ,
12
+ "pygments_lexer" : " ipython3" ,
13
+ "version" : " 3.8.5"
14
+ },
15
+ "orig_nbformat" : 2 ,
16
+ "kernelspec" : {
17
+ "name" : " python385jvsc74a57bd09ceeaccb2d4b8fc120d52b4319c20d729c878e30972ae0f296691445edd93f69" ,
18
+ "display_name" : " Python 3.8.5 32-bit"
19
+ }
20
+ },
21
+ "nbformat" : 4 ,
22
+ "nbformat_minor" : 2 ,
23
+ "cells" : [
24
+ {
25
+ "source" : [
26
+ " # Manipulate data in Azure SQL Database using Python and Jupyter Notebook"
27
+ ],
28
+ "cell_type" : " markdown" ,
29
+ "metadata" : {}
30
+ },
31
+ {
32
+ "source" : [
33
+ " ## Import the library"
34
+ ],
35
+ "cell_type" : " markdown" ,
36
+ "metadata" : {}
37
+ },
38
+ {
39
+ "cell_type" : " code" ,
40
+ "execution_count" : null ,
41
+ "metadata" : {},
42
+ "outputs" : [],
43
+ "source" : [
44
+ " import pyodbc"
45
+ ]
46
+ },
47
+ {
48
+ "source" : [
49
+ " ## Open the connection"
50
+ ],
51
+ "cell_type" : " markdown" ,
52
+ "metadata" : {}
53
+ },
54
+ {
55
+ "cell_type" : " code" ,
56
+ "execution_count" : null ,
57
+ "metadata" : {},
58
+ "outputs" : [],
59
+ "source" : [
60
+ " server = '<server>.database.windows.net'\n " ,
61
+ " database = '<database>'\n " ,
62
+ " username = '<username>'\n " ,
63
+ " password = '<password>'\n " ,
64
+ " driver= '{ODBC Driver 17 for SQL Server}'\n " ,
65
+ " conn = pyodbc.connect('DRIVER=' + driver + ';SERVER=' +\n " ,
66
+ " server + ';PORT=1433;DATABASE=' + database +\n " ,
67
+ " ';UID=' + username + ';PWD=' + password)"
68
+ ]
69
+ },
70
+ {
71
+ "source" : [
72
+ " ## Create a cursor object"
73
+ ],
74
+ "cell_type" : " markdown" ,
75
+ "metadata" : {}
76
+ },
77
+ {
78
+ "cell_type" : " code" ,
79
+ "execution_count" : null ,
80
+ "metadata" : {},
81
+ "outputs" : [],
82
+ "source" : [
83
+ " cursor = conn.cursor()"
84
+ ]
85
+ },
86
+ {
87
+ "source" : [
88
+ " ## Insert a new movie"
89
+ ],
90
+ "cell_type" : " markdown" ,
91
+ "metadata" : {}
92
+ },
93
+ {
94
+ "cell_type" : " code" ,
95
+ "execution_count" : null ,
96
+ "metadata" : {},
97
+ "outputs" : [],
98
+ "source" : [
99
+ " cursor.execute(\" INSERT INTO movies \"\n " ,
100
+ " \" (movie_title, released_year, runtime, genre, rating, director, \"\n " ,
101
+ " \" star1, star2, number_of_votes, gross) \"\n " ,
102
+ " \" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\" , \n " ,
103
+ " (\" The Imitation Game\" , 2014, 114, \" Biography\" , 80, \n " ,
104
+ " \" Morten Tyldum\" , \" Benedict Cumberbatch\" , \" Keira Knightley\" , \n " ,
105
+ " 685201, 91125683))"
106
+ ]
107
+ },
108
+ {
109
+ "source" : [
110
+ " ## Commit the changes"
111
+ ],
112
+ "cell_type" : " markdown" ,
113
+ "metadata" : {}
114
+ },
115
+ {
116
+ "cell_type" : " code" ,
117
+ "execution_count" : null ,
118
+ "metadata" : {},
119
+ "outputs" : [],
120
+ "source" : [
121
+ " conn.commit()"
122
+ ]
123
+ },
124
+ {
125
+ "source" : [
126
+ " ## Read data"
127
+ ],
128
+ "cell_type" : " markdown" ,
129
+ "metadata" : {}
130
+ },
131
+ {
132
+ "cell_type" : " code" ,
133
+ "execution_count" : null ,
134
+ "metadata" : {},
135
+ "outputs" : [],
136
+ "source" : [
137
+ " # Read data\n " ,
138
+ " cursor.execute(\" SELECT \"\n " ,
139
+ " \" movie_id, movie_title, director \"\n " ,
140
+ " \" FROM movies\" )\n " ,
141
+ " rows = cursor.fetchall()\n " ,
142
+ " \n " ,
143
+ " # Print data\n " ,
144
+ " for row in rows:\n " ,
145
+ " print(row)"
146
+ ]
147
+ },
148
+ {
149
+ "source" : [
150
+ " ## Update a data row in the table"
151
+ ],
152
+ "cell_type" : " markdown" ,
153
+ "metadata" : {}
154
+ },
155
+ {
156
+ "cell_type" : " code" ,
157
+ "execution_count" : null ,
158
+ "metadata" : {},
159
+ "outputs" : [],
160
+ "source" : [
161
+ " cursor.execute(\" UPDATE movies \"\n " ,
162
+ " \" SET genre = ? \"\n " ,
163
+ " \" WHERE movie_title = ?\" , \n " ,
164
+ " (\" Biography\" , \" The Theory of Everything\" ))\n " ,
165
+ " \n " ,
166
+ " print(\" Updated\" ,cursor.rowcount,\" row(s) of data.\" )"
167
+ ]
168
+ },
169
+ {
170
+ "cell_type" : " code" ,
171
+ "execution_count" : null ,
172
+ "metadata" : {},
173
+ "outputs" : [],
174
+ "source" : [
175
+ " # Read data\n " ,
176
+ " cursor.execute(\" SELECT \"\n " ,
177
+ " \" movie_id, movie_title, genre \"\n " ,
178
+ " \" FROM movies \"\n " ,
179
+ " \" WHERE movie_title = ?\" ,\n " ,
180
+ " (\" The Theory of Everything\" , ))\n " ,
181
+ " rows = cursor.fetchall()\n " ,
182
+ " print(rows[0])"
183
+ ]
184
+ },
185
+ {
186
+ "source" : [
187
+ " ## Delete data"
188
+ ],
189
+ "cell_type" : " markdown" ,
190
+ "metadata" : {}
191
+ },
192
+ {
193
+ "cell_type" : " code" ,
194
+ "execution_count" : null ,
195
+ "metadata" : {},
196
+ "outputs" : [],
197
+ "source" : [
198
+ " cursor.execute(\" DELETE FROM movies \"\n " ,
199
+ " \" WHERE genre = ?\" , \n " ,
200
+ " (\" Horror\" , ))\n " ,
201
+ " print(\" Deleted\" ,cursor.rowcount,\" row(s) of data.\" )"
202
+ ]
203
+ },
204
+ {
205
+ "source" : [
206
+ " ## Order data"
207
+ ],
208
+ "cell_type" : " markdown" ,
209
+ "metadata" : {}
210
+ },
211
+ {
212
+ "cell_type" : " code" ,
213
+ "execution_count" : null ,
214
+ "metadata" : {},
215
+ "outputs" : [],
216
+ "source" : [
217
+ " # Read data\n " ,
218
+ " cursor.execute(\" SELECT movie_title, released_year, genre, gross \"\n " ,
219
+ " \" FROM movies \"\n " ,
220
+ " \" WHERE genre = ? AND released_year BETWEEN ? AND ? \"\n " ,
221
+ " \" ORDER BY released_year ASC, gross DESC\" ,\n " ,
222
+ " (\" Action\" , 2009, 2019))\n " ,
223
+ " rows = cursor.fetchall()\n " ,
224
+ " \n " ,
225
+ " # Print data\n " ,
226
+ " for row in rows:\n " ,
227
+ " print(row)"
228
+ ]
229
+ },
230
+ {
231
+ "source" : [
232
+ " ## Use the *MAX()* function"
233
+ ],
234
+ "cell_type" : " markdown" ,
235
+ "metadata" : {}
236
+ },
237
+ {
238
+ "cell_type" : " code" ,
239
+ "execution_count" : null ,
240
+ "metadata" : {},
241
+ "outputs" : [],
242
+ "source" : [
243
+ " # Read data\n " ,
244
+ " cursor.execute(\" SELECT MAX(gross) \"\n " ,
245
+ " \" FROM movies \"\n " ,
246
+ " \" WHERE director = ? \" ,\n " ,
247
+ " (\" Martin Scorsese\" , ))\n " ,
248
+ " rows = cursor.fetchall()\n " ,
249
+ " \n " ,
250
+ " # Print data\n " ,
251
+ " print(rows[0])"
252
+ ]
253
+ },
254
+ {
255
+ "source" : [
256
+ " ## Close the connection"
257
+ ],
258
+ "cell_type" : " markdown" ,
259
+ "metadata" : {}
260
+ },
261
+ {
262
+ "cell_type" : " code" ,
263
+ "execution_count" : null ,
264
+ "metadata" : {},
265
+ "outputs" : [],
266
+ "source" : [
267
+ " conn.commit()\n " ,
268
+ " cursor.close()\n " ,
269
+ " conn.close()"
270
+ ]
271
+ }
272
+ ]
273
+ }
0 commit comments