-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_basics.py
59 lines (46 loc) · 1.56 KB
/
mysql_basics.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
import mysql.connector
from mysql.connector import errorcode
print("mysql")
# Connect to database
# try:
# db = mysql.connector.connect(
# host = "localhost", # Since mysql is running on same computer
# user = "root",
# passwd = "Gradebook",
# database = "Gradebook"
# )
# mycursor = db.cursor()
# except mysql.connector.Error as err:
# if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
# print("Something is wrong with your username or password")
# elif err.errno == errorcode.ER_BAD_DB_ERROR:
# print("Databse does not exist")
# # cursor object using above database
# mycursor.execute("CREATE DATABASE Gradebook")
# else:
# print(err)
# else:
# db.close()
db = mysql.connector.connect(
host = "localhost", # Since mysql is running on same computer
user = "root",
passwd = "Gradebook",
database = "Gradebook"
)
# cursor object using above database
mycursor = db.cursor()
# Create a database
#mycursor.execute("CREATE DATABASE Gradebook")
# Create a classes table
#mycursor.execute("CREATE TABLE Classes (class_id int PRIMARY KEY AUTO_INCREMENT, class_name VARCHAR(50), Year int UNSIGNED)")
# Add rows to table
#mycursor.execute("INSERT INTO classes (class_name, Year) VALUES(%s,%s)", ("Class A", 2021))
# Print data from table
mycursor.execute("SELECT * FROM classes")
# To commit data to table
#db.commit()
# Describe a table
#mycursor.execute("DESCRIBE classes")
# prints information what mycursor got
for x in mycursor:
print(x)