-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase_manage.py
94 lines (69 loc) · 2.64 KB
/
database_manage.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
82
83
84
85
86
87
88
89
90
91
92
93
94
import sqlite3
import csv
import pandas as pd
import openpyxl
# creating the database
conn = sqlite3.connect('mass_mailing.db')
# create a cursor for database
cursor = conn.cursor()
# create table
def creation():
cursor.execute(" CREATE TABLE mail_list(\n"
" Name text,\n"
" Email text,\n"
" DOB integer\n"
" )")
# NULL, INTEGER, REAL , TEXT, BLOB(IMAGES, MP3 FILE)
# cursor.fetchall() to return all the queries in a list
def input_(name, email, dob): # function to append new data into table
cursor.execute("INSERT INTO mail_list VALUES (?, ?, ?)", [name, email, dob])
conn.commit()
def delete_(query, column): # function to delete data based on column
if column == 'DOB':
cursor.execute("DELETE FROM mail_list WHERE DOB = ? ", (query,))
elif column == 'Email':
cursor.execute("DELETE FROM mail_list WHERE Email = ? ", (query,))
elif column == 'Name':
cursor.execute("DELETE FROM mail_list WHERE Name = ? ", (query,))
conn.commit()
def get_query(): # function returns the contents of the table
cursor.execute("SELECT * FROM mail_list")
l = cursor.fetchall()
return l
def view_specific(query, column):
if column == 'DOB':
cursor.execute("SELECT rowid, * FROM mail_list WHERE DOB = ? ", (query,))
elif column == 'Email':
cursor.execute("SELECT rowid, * FROM mail_list WHERE Email = ? ", (query,))
elif column == 'Name':
cursor.execute("SELECT rowid, * FROM mail_list WHERE Name = ? ", (query,))
l = cursor.fetchall()
return l
def reset(): # Deletes all data in the table
cursor.execute("DELETE FROM mail_list")
conn.commit()
def print_csv(l, t):
l = [list(i) for i in l]
if t == 'CSV':
with open('file_database.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(l)
elif t == 'Excel':
df = pd.DataFrame(l,
columns=['Name', 'Email', 'DOB'])
df.to_excel('database_file.xlsx', index=False)
def initialize(): # initializes data with default dataset.
array = [
("Anwesan de", 'f20190518@hyderabad.bits-pilani.ac.in', 19092000),
("Aditya Goyal", 'wdenny@gmail.com', 12042001),
("Abhinav gupta", 'f20190380@hyderabad.bits-pilani.ac.in', 16012000),
('Nirav jayesh parmar', 'f20190540@hyderabad.bits-pilani.ac.in', 7022002)
]
cursor.executemany("INSERT INTO mail_list VALUES (?, ?, ?)", array)
conn.commit()
# reset()
# creation()
#initialize()
#print(get_query())
#print(view_specific(16012000, 'DOB'))
#print_csv(get_query())