-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.py
81 lines (61 loc) · 1.88 KB
/
sql.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
70
71
72
73
74
75
76
77
78
79
80
81
import mysql.connector
import simplejson as json
with open("config.json", "r") as f:
config = json.load(f)
def selectQuery(query):
"""Executes `SELECT` query provided and returns the output in JSON\n
Connects to a predefined `Database` from `config.json`"""
json_data = []
db = config["DATABASE"]
mydb = mysql.connector.connect(
host=db["HOST"],
user=db["USERNAME"],
password=db["PASSWORD"],
database=db["DB"],
)
mycursor = mydb.cursor(dictionary=True)
mycursor.execute(query)
res = mycursor.fetchall()
for result in res:
json_data.append(dict(result))
mydb.close()
return json_data
def insertQuery(query):
"""Executes `INSERT` query provided and returns `mycursor.rowcount`\n
Connects to a predefined `Database` from `config.json`"""
db = config["DATABASE"]
mydb = mysql.connector.connect(
host=db["HOST"],
user=db["USERNAME"],
password=db["PASSWORD"],
database=db["DB"],
)
mycursor = mydb.cursor()
mycursor.execute(query)
mydb.commit()
return mycursor.rowcount
def insert_escaped_query(query):
"""Executes `INSERT` query `mycursor.execute("", (query))` provided and returns `mycursor.rowcount`\n
Connects to a predefined `Database` from `config.json`"""
db = config["DATABASE"]
mydb = mysql.connector.connect(
host=db["HOST"],
user=db["USERNAME"],
password=db["PASSWORD"],
database=db["DB"],
)
mycursor = mydb.cursor()
mycursor.execute("", (query))
mydb.commit()
return mycursor.rowcount
def syncQuery(query):
db = config["DATABASE"]
mydb = mysql.connector.connect(
host=db["HOST"],
user=db["USERNAME"],
password=db["PASSWORD"],
)
mycursor = mydb.cursor()
mycursor.execute(query)
mydb.commit()
return mycursor.rowcount