-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore_data.py
More file actions
51 lines (46 loc) · 1.44 KB
/
store_data.py
File metadata and controls
51 lines (46 loc) · 1.44 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
import mysql.connector
from mysql.connector import Error
def create_connection():
connection = None
try:
connection = mysql.connector.connect(
host="mysql1",
user="testuser",
passwd="test",
database="testdb"
)
print("Connection to MySQL DB successful")
except Error as e:
print(f"The error '{e}' occurred")
return connection
def create_table(connection):
create_table_query = """
CREATE TABLE IF NOT EXISTS menteestasks (
id INT AUTO_INCREMENT,
name TEXT NOT NULL,
web TEXT DEFAULT 'n',
app TEXT DEFAULT 'n',
sysad TEXT DEFAULT 'n',
PRIMARY KEY (id)
)
"""
cursor = connection.cursor()
try:
cursor.execute(create_table_query)
connection.commit()
print("Table created successfully")
except Error as e:
print(f"The error '{e}' occurred")
def insert_data(connection, name, web, app, sysad):
insert_query = f"INSERT INTO `menteestask` (`id`, `name`, `web`, `app`, `sysad`) VALUES (NULL, '{name}', '{web}', '{app}', '{sysad}'); "
cursor = connection.cursor()
try:
cursor.execute(insert_query)
connection.commit()
print("Data inserted successfully")
except Error as e:
print(f"The error '{e}' occurred")
conn = create_connection()
if conn is not None:
create_table(conn)
insert_data(conn, "aswin", 'n', 'n', 'y')