forked from aleksati/standstill-station
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql-cheatsheet.py
More file actions
103 lines (85 loc) · 3.61 KB
/
sql-cheatsheet.py
File metadata and controls
103 lines (85 loc) · 3.61 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import mysql.connector
import yaml
with open("config.yml", "r") as f:
config = yaml.safe_load(f)
db = mysql.connector.connect(
host="localhost",
user=config["sql"]["user"],
passwd=config["sql"]["password"],
database=config["sql"]["database"]
)
cursor = db.cursor()
# --------Create a database-------------
# Run once then add to connect string
# cursor.execute("CREATE DATABASE standstill")
# --------------Create a table structure (slags excel ark table)-----------------
# cursor.execute("CREATE TABLE Person (name VARCHAR(50), age smallint UNSIGNED, personID int PRIMARY KEY AUTO_INCREMENT)")
# ---GET the Person table created and its types. What does the table it self look like?---
#cursor.execute("DESCRIBE Person)
# You can also iterate on it.
# for x in cursor:
# print(x)
# ---INSERT/add values into Table---
# cursor.execute("INSERT INTO Person (name, age) VALUES (%s,%s)", ("Joe", 56))
# db.commit()
# ---GET everything from my Person Table and interate over it in the console.---
# cursor.execute("SELECT * FROM standstillUser")
# for x in cursor:
# print(x)
# ---- REMOVE STUFF -----
# Remove one column from a Table
#cursor.execute("ALTER TABLE standstillUser DROP musicScore") # For eksempel..
#db.commit()
# Change the name of a column
#cursor.execute("ALTER TABLE standstillUser CHANGE musicScore musicScoreNew VARCHAR(50)") # For eksempel..
#db.commit()
# # Delete all rows from specific Table
# cursor.execute("DELETE from standstillUser")
# cursor.execute("DELETE from standstillRealTime")
# # When deleting all data, reset auto_increment to 1
# cursor.execute("ALTER TABLE standstillUser AUTO_INCREMENT = 1")
# cursor.execute("ALTER TABLE standstillRealTime AUTO_INCREMENT = 1")
# db.commit()
# Delete rows based on id from specific Table
# cursor.execute("DELETE from standstillUser where id = 2") # For eksempel..
# db.commit()
# Delete database (But remembe to remove the database string in the connectro string first!)
# cursor.execute("DROP DATABASE sakila")
#db.commit()
# --------- Creating the standstill Tables ----------------
# Creating the Tables only needs to happen once. And I did it in the sql-cheasheet.py file.
# try: # to create a table for storing real-time data in MySQL database
# cursor.execute(
# """
# CREATE TABLE standstillUser
# (
# id INT unsigned NOT NULL AUTO_INCREMENT,
# standstillUserID INT unsigned,
# age INT unsigned,
# language VARCHAR(2),
# musicScore FLOAT,
# silenceScore FLOAT,
# feedbackMusic INT unsigned,
# feedbackStandstill INT unsigned,
# PRIMARY KEY (id)
# )
# """
# )
# except mysql.connector.errors.ProgrammingError:
# pass
# try:
# cursor.execute(
# """
# CREATE TABLE standstillRealTime
# (
# id INT unsigned NOT NULL AUTO_INCREMENT,
# standstillUserID INT unsigned,
# genre VARCHAR(50),
# date TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
# w FLOAT(7,6) NOT NULL,
# PRIMARY KEY (id)
# )
# """
# )
# except mysql.connector.errors.ProgrammingError:
# pass