-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path72.mysql3.py
More file actions
56 lines (45 loc) · 1.71 KB
/
72.mysql3.py
File metadata and controls
56 lines (45 loc) · 1.71 KB
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
# 71 . Python MySql p2
"""
# The ORDER BY statement to sort the result in ascending or descending order.
# The ORDER BY keyword sorts the result ascending by default. To sort the result in
descending order, use the DESC keyword
# Delete: delete records from an existing table by using the "DELETE FROM" statement
# Important!: 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!
# delete an existing table by using the "DROP TABLE" statement.
# If the table you want to delete is already deleted, or for any other reason does not exist, you can
use the IF EXISTS keyword to avoid getting an error.
"""
import mysql.connector
con = mysql.connector.connect(
host = "localhost",
user = "root",
password= "omarroot000",
database= "pythondb"
)
mycursor = con.cursor()
q = "SELECT * FROM categories ORDER BY name DESC"
mycursor.execute(q)
result = mycursor.fetchall()
for x in result:
print(x)
q = "DELETE FROM categories WHERE name = 'Javascript'"
mycursor.execute(q)
con.commit()
print(mycursor.rowcount,"record(s) deleted")
mycursor.execute("SELECT * FROM categories ORDER BY name DESC")
result = mycursor.fetchall()
for x in result:
print(x)
#mycursor.execute("CREATE TABLE categories_tt(name VARCHAR(255), description VARCHAR(255))")
mycursor.execute("SHOW TABLES")
print("Tables:")
for tbl in mycursor:
print(tbl)
mycursor.execute("DROP TABLE categories_tt")
mycursor.execute("SHOW TABLES")
print("Tables:")
for tbl in mycursor:
print(tbl)