-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdb_manager.py
More file actions
60 lines (50 loc) · 1.68 KB
/
db_manager.py
File metadata and controls
60 lines (50 loc) · 1.68 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
import sqlite3
def create_db():
# Step 1: Connect to the SQLite database (or create a new one if it doesn't exist)
conn = sqlite3.connect('emaildb.sqlite3')
# Step 2: Create a cursor object
cursor = conn.cursor()
# Step 3: Define an SQL command to create the table
create_table_sql = '''
CREATE TABLE IF NOT EXISTS mytable (
id INTEGER PRIMARY KEY,
subject TEXT,
sender_name TEXT,
sender_email TEXT,
receiver_name TEXT,
receiver_email TEXT,
attachment_name TEXT,
content_type TEXT,
datetime TEXT
)'''
# Step 4: Execute the SQL command to create the table
cursor.execute(create_table_sql)
# Step 5: Commit the changes and close the database connection
conn.commit()
conn.close()
print("Database and table created successfully.")
def store_data(data_dict):
# Connect to the SQLite database
conn = sqlite3.connect('emaildb.sqlite3')
cursor = conn.cursor()
# Define data to be inserted
data_to_insert = (
data_dict['subject'],
data_dict['sender_name'],
data_dict['sender_email'],
data_dict['receiver_name'],
data_dict['receiver_email'],
data_dict['attachment_name'],
data_dict['content_type'],
data_dict['datetime'],
)
# Insert data into the table
insert_sql = '''
INSERT INTO mytable (subject, sender_name, sender_email, receiver_name, receiver_email, attachment_name, content_type, datetime)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
'''
cursor.execute(insert_sql, data_to_insert)
# Commit the changes and close the database connection
conn.commit()
conn.close()
print("Data inserted in db successfully.")