-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQL.py
More file actions
81 lines (69 loc) · 2.49 KB
/
MySQL.py
File metadata and controls
81 lines (69 loc) · 2.49 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
# from pip._internal import main
# main(['install','mysql-connector-python-rf'])
import mysql.connector
import self as self
class DB:
""" Creating a DB, (host, user, password, db) """
def __init__(self, host, user, password, db):
self.host = host
self.user = user
self.password = password
self.db = db
def conn(self):
""" Connection to a database """
try:
c = mysql.connector.connect(host=self.host, user=self.user, password=self.password, database=self.db)
return c
except:
print("Connection failed.")
exit(1)
def select(self, table, selected):
""" MySQL SELECT method (SELECT {id} from {table}) """
cn = self.conn()
cur = cn.cursor()
cur.execute("SELECT {0} from {1}".format(selected, table))
return cur.fetchone()
def createDB(self, name):
""" Create a db, (CREATE DATABASE {name}) """
cn = self.conn()
cur = cn.cursor()
cur.execute("CREATE DATABASE {0}".format(name))
def checkAllDB(self):
""" Check all DB """
cn = self.conn()
cur = cn.cursor()
cur.execute("SHOW DATABASES")
for x in cur:
return x
def checkTable(self):
""" Check all tables """
cn = self.conn()
cur = cn.cursor()
cur.execute("SHOW TABLES")
for x in cur:
return x
def createTable(self, name, arg):
""" Create table (CREATE TABLE {name} ({element}))"""
cn = self.conn()
cur = cn.cursor(buffered=True)
cur.execute("CREATE TABLE {0} ({1})".format(name, arg))
def removeTable(self, name):
""" Remove a table {name} """
cn = self.conn()
cur = cn.cursor()
cur.execute("DROP TABLE {0}".format(name))
def update(self, table, arg):
""" Update table """
cn = self.conn()
cur = cn.cursor()
cur.execute("UPDATE {0} SET {1}".format(table, arg))
def join(self, table1, table2, how):
""" Join two tables, how=RIGHT | LEFT """
cn = self.conn()
cur = cn.cursor()
if how == "RIGHT":
cur.execute("SELECT {0}.name AS user, {1}.name AS favorite FROM {0} RIGHT JOIN {1} ON {0}.fav = {0}.id".format(table1, table2))
elif how == "LEFT":
cur.execute("SELECT {0}.name AS user, {1}.name AS favorite FROM {0} LEFT JOIN {1} ON {0}.fav = {0}.id".format(table1, table2))
else:
return False