-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_functions.py
47 lines (37 loc) · 1.02 KB
/
mysql_functions.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
from mysql.connector import connect, Error
connection = ""
cursor = ""
def mysql_connect():
try:
global connection, cursor
connection = connect(
host="localhost",
user="root",
password="root",
database="progetto",
)
cursor = connection.cursor(buffered=True)
except Error as e:
print(e)
def exec_query(query_string, commit=False, fetchOne=False, fetchAll=False):
try:
res = cursor.execute(query_string)
if commit:
connection.commit()
if fetchOne:
res = cursor.fetchone()
if fetchAll:
res = cursor.fetchall()
# print(res)
return res
except Error as e:
print(e)
def insert_many(query_string, records):
cursor.execute("SET GLOBAL max_allowed_packet=10000000")
try:
cursor.executemany(query_string, records)
connection.commit()
except Error as e:
print(e)
def close_connection():
connection.close()