-
Notifications
You must be signed in to change notification settings - Fork 5
/
07Select.py
69 lines (48 loc) · 1.69 KB
/
07Select.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="root",
database="mypython_test_db"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM employee")
myresult = mycursor.fetchall()
print("**************All Records*******")
for x in myresult:
print(x)
#Where Condition
sql1 = "SELECT * FROM employee WHERE address ='Pune'"
mycursor.execute(sql1)
myresult = mycursor.fetchall()
print("***********Records which satisfy where condition ***********")
for x in myresult:
print(x)
print("**********Record which have 'a' in name************")
sql2 = "SELECT * FROM employee WHERE name LIKE '%ha%'"
mycursor.execute(sql2)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
#Prevent SQL Injection
print("**********Prevent SQL Injection******************************");
sql3 = "SELECT * FROM employee WHERE name = %s"
name = ("Salman Khan", )
mycursor.execute(sql3, name)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
print("********Order By**************");
sql4 = "SELECT * FROM employee ORDER BY name"
#To have DESC order give DESC after the name in above query
mycursor.execute(sql4)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
print("**************Delete Record************************")
sql5 = "DELETE FROM employee WHERE name = 'Amey Wagh'"
mycursor.execute(sql5)
mydb.commit()
#Notice the statement: mydb.commit(). It is required to make the changes, otherwise no changes are made to the table.
#Notice the WHERE clause in the DELETE syntax: The WHERE clause specifies which record(s) that should be deleted. If you omit the WHERE clause, all records will be deleted!
print(mycursor.rowcount, "record(s) deleted")