-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql_conection.py
More file actions
65 lines (51 loc) · 1.31 KB
/
mysql_conection.py
File metadata and controls
65 lines (51 loc) · 1.31 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
from mysql.connector import connect, Error
def initiate_connection(user="root", password="Aditya@1", database="covid19"):
mycon = connect(host="localhost", user=user, password=password, database=database)
mycur = mycon.cursor()
return mycon, mycur
def insert_data(mycon, query, val):
try:
cursor = mycon.cursor()
cursor.execute(query, val)
mycon.commit()
return 0
except Error as e:
print(e)
return 1
def create_table(mycon, query):
try:
cursor = mycon.cursor()
cursor.execute(query)
mycon.commit()
return 0
except Error as e:
print(e)
return 1
def select_data(mycon, query):
try:
cursor = mycon.cursor()
cursor.execute(query)
return cursor.fetchall()
except Error as e:
print(e)
return 1
def update_data(mycon, query, val):
try:
cursor = mycon.cursor()
cursor.execute(query, val)
mycon.commit()
return 0
except Error as e:
print(e)
return 1
def delete_data(mycon, query, val):
try:
cursor = mycon.cursor()
cursor.execute(query, val)
mycon.commit()
return 0
except Error as e:
print(e)
return 1
def close_connection(mycon):
mycon.close()