Deleting Records From a Table with Python

Python 3: Automating Your Job Tasks Superhero Level: Automate Database Tasks with Python 3
1 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€91.83
List Price:  €128.57
You save:  €36.73
£77.40
List Price:  £108.37
You save:  £30.96
CA$137.71
List Price:  CA$192.80
You save:  CA$55.09
A$149.59
List Price:  A$209.44
You save:  A$59.84
S$134.48
List Price:  S$188.28
You save:  S$53.80
HK$780.99
List Price:  HK$1,093.42
You save:  HK$312.43
CHF 88.87
List Price:  CHF 124.42
You save:  CHF 35.55
NOK kr1,092.25
List Price:  NOK kr1,529.19
You save:  NOK kr436.94
DKK kr685.52
List Price:  DKK kr959.75
You save:  DKK kr274.23
NZ$166.28
List Price:  NZ$232.81
You save:  NZ$66.52
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.92
৳11,741.01
List Price:  ৳16,437.88
You save:  ৳4,696.87
₹8,372.21
List Price:  ₹11,721.43
You save:  ₹3,349.22
RM468.68
List Price:  RM656.18
You save:  RM187.49
₦166,379.78
List Price:  ₦232,938.35
You save:  ₦66,558.56
₨27,788.72
List Price:  ₨38,905.32
You save:  ₨11,116.60
฿3,633.29
List Price:  ฿5,086.75
You save:  ฿1,453.46
₺3,302.46
List Price:  ₺4,623.58
You save:  ₺1,321.12
B$559.99
List Price:  B$784.01
You save:  B$224.02
R1,827.57
List Price:  R2,558.67
You save:  R731.10
Лв179.84
List Price:  Лв251.79
You save:  Лв71.94
₩138,966.10
List Price:  ₩194,558.10
You save:  ₩55,592
₪366.35
List Price:  ₪512.91
You save:  ₪146.55
₱5,839.25
List Price:  ₱8,175.19
You save:  ₱2,335.93
¥15,746.92
List Price:  ¥22,046.32
You save:  ¥6,299.40
MX$1,806.20
List Price:  MX$2,528.76
You save:  MX$722.55
QR364.68
List Price:  QR510.57
You save:  QR145.88
P1,353.12
List Price:  P1,894.42
You save:  P541.30
KSh12,839.91
List Price:  KSh17,976.40
You save:  KSh5,136.48
E£4,821.16
List Price:  E£6,749.82
You save:  E£1,928.65
ብር5,728.13
List Price:  ብር8,019.61
You save:  ብር2,291.48
Kz87,254.77
List Price:  Kz122,160.17
You save:  Kz34,905.40
CLP$94,165.85
List Price:  CLP$131,835.96
You save:  CLP$37,670.10
CN¥726.92
List Price:  CN¥1,017.72
You save:  CN¥290.80
RD$5,915.94
List Price:  RD$8,282.56
You save:  RD$2,366.61
DA13,459.55
List Price:  DA18,843.91
You save:  DA5,384.36
FJ$223.54
List Price:  FJ$312.97
You save:  FJ$89.42
Q776.39
List Price:  Q1,086.99
You save:  Q310.59
GY$20,905.03
List Price:  GY$29,267.89
You save:  GY$8,362.85
ISK kr13,755.22
List Price:  ISK kr19,257.87
You save:  ISK kr5,502.64
DH984.58
List Price:  DH1,378.46
You save:  DH393.87
L1,768.63
List Price:  L2,476.15
You save:  L707.52
ден5,656.13
List Price:  ден7,918.82
You save:  ден2,262.68
MOP$803.72
List Price:  MOP$1,125.24
You save:  MOP$321.52
N$1,832.80
List Price:  N$2,566
You save:  N$733.19
C$3,677.72
List Price:  C$5,148.96
You save:  C$1,471.23
रु13,375.42
List Price:  रु18,726.12
You save:  रु5,350.70
S/374.16
List Price:  S/523.84
You save:  S/149.68
K391.36
List Price:  K547.92
You save:  K156.56
SAR375.04
List Price:  SAR525.08
You save:  SAR150.03
ZK2,550.54
List Price:  ZK3,570.86
You save:  ZK1,020.31
L456.59
List Price:  L639.25
You save:  L182.65
Kč2,317.81
List Price:  Kč3,245.03
You save:  Kč927.22
Ft35,847.25
List Price:  Ft50,187.59
You save:  Ft14,340.33
SEK kr1,067.89
List Price:  SEK kr1,495.09
You save:  SEK kr427.19
ARS$92,404.20
List Price:  ARS$129,369.57
You save:  ARS$36,965.37
Bs690.46
List Price:  Bs966.68
You save:  Bs276.21
COP$404,587
List Price:  COP$566,437.98
You save:  COP$161,850.98
₡52,430.08
List Price:  ₡73,404.21
You save:  ₡20,974.13
L2,474.80
List Price:  L3,464.82
You save:  L990.01
₲755,565.28
List Price:  ₲1,057,821.62
You save:  ₲302,256.33
$U4,029.23
List Price:  $U5,641.08
You save:  $U1,611.85
zł393.80
List Price:  zł551.34
You save:  zł157.53
Already have an account? Log In

Transcript

Okay, so we currently have these employees right here in our database. Let's say that, for some reason, we want to delete all the employees that have a salary that is greater than $50,000 from this table, so that would be Emily, David's right here with a salary of 59,000. And also jet though with a salary of 55,000. In order to do that, SQL provides us with the delete from command. So we have delete from my staff, the name of the schema dot employees, the name of the table, and of the condition where salary, the name of the column right here is greater than $50,000. And of course, at the end, we must enter the semi colon.

Okay, now let's go back into the Python interpreter, and let's establish a new connection. To our database. Okay, now let's initialize the cursor. And finally, let's use the execute method on this cursor. So cursor, dot execute. And in between the parentheses of execute in between double quotes, we again copy and paste in this command.

Okay, let's hit enter. Now let's also commit our changes. So connection, dot commit, and connection dot close. And at this point, we should have three employees left in the table. So let's check this. And indeed We do.

The employees having salaries greater than $50,000 were deleted from the table. I'll see you in the next lecture where we are going to start querying our database using various conditions. And I think this is going to be pretty interesting. So I'll see you soon.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.