-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-10_DB_XL_Lakshay.py
190 lines (157 loc) · 5 KB
/
11-10_DB_XL_Lakshay.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'''
11-10-2020 Database Connectivity Program with Excel support
Database Name: xii_cs
Table Name: stud
CREATE TABLE stud (admno int(11) primary key AUTO_INCREMENT,
name varchar(30) DEFAULT NULL,
sex char(1) DEFAULT 'M',
cls int(11) DEFAULT NULL,
section char(1) DEFAULT NULL,
)
'''
import mysql.connector as conn
import datetime as d
import xlwt as xl
def insertRecord():
db=conn.connect(host='localhost', user='root', password='pankaj', database='xii_cs')
myc=db.cursor()
admno=int(input('Enter admission number :'))
name=input('Enter your name :')
sex=input('Enter your gender (M/F/T) :')
cls=int(input('Enter your class in number :'))
section=input('Enter your section (A/B/C/D) :')
name=name.upper()
sex=sex.upper()
section=section.upper()
sql="insert into stud values({},'{}','{}',{},'{}')".format(admno,name,sex,cls,section)
myc.execute(sql)
db.commit()
print('Record inserted . . .')
myc.close()
db.close()
def showRecord():
db=conn.connect(host='localhost', user='root', password='pankaj', database='xii_cs')
myc=db.cursor()
sql="select * from stud"
myc.execute(sql)
res=myc.fetchall()
k=myc.rowcount
print('No of records are :',k)
N=45
print('Adm\tName\t\tSex\tClass\tSection')
print('='*N)
for record in res:
print(record[0],"\t",record[1],"\t",record[2],"\t",record[3],"\t",record[4])
print('='*N)
myc.close()
db.close()
def updateRecord():
db=conn.connect(host='localhost', user='root', password='pankaj', database='xii_cs')
myc=db.cursor()
adm=int(input('Enter admission number to update record :'))
print('Which detail you want to update?')
print('Enter 1. Gender\t 2. Class\t 3. Section')
k=int(input('Enter your choice (1/2/3) :'))
if k==1:
newsex=input('Enter sex :')
sql="update stud set sex='{}' where admno={}".format(newsex,adm)
elif k==2:
newcls=int(input('Enter class :'))
sql="update stud set cls={} where admno={}".format(newcls,adm)
elif k==3:
newsection=input('Enter section :')
sql="update stud set section='{}' where admno={}".format(newsection,adm)
myc.execute(sql)
db.commit()
print('Record updated successfully . . .')
myc.close()
db.close()
def deleteRecord():
db=conn.connect(host='localhost', user='root', password='pankaj', database='xii_cs')
myc=db.cursor()
adm=int(input('Enter admission number to delete record :'))
sql="select * from stud where admno={}".format(adm)
myc.execute(sql)
res=myc.fetchall()
norec=myc.rowcount
if norec==1:
sql="delete from stud where admno={}".format(adm)
myc.execute(sql)
db.commit()
print('Record deleted successfully . . .')
else:
print('Sorry record does not exist')
myc.close()
db.close()
def getDate_Time():
s=''
dy=str(d.datetime.now().day)
mo=str(d.datetime.now().month)
yr=str(d.datetime.now().year)
hr=str(d.datetime.now().hour)
mi=str(d.datetime.now().minute)
se=str(d.datetime.now().second)
s=dy+mo+yr+hr+mi+se
return s
def genExcel():
wb = xl.Workbook() # Workbook is created
sheet1 = wb.add_sheet('All_student') # add_sheet is used to create sheet.
db=conn.connect(host='localhost', user='root', password='pankaj', database='xii_cs')
myc=db.cursor()
sql="desc stud"
myc.execute(sql)
myresult=myc.fetchall()
Field_list=list()
for data in myresult:
Field_list.append(data[0])
#print(Field_list)
#Writing heading of table in Excel
style=xl.easyxf('alignment: horizontal center')
k=0
for heading in Field_list:
#print(k,heading)
sheet1.write(0,k,heading,style=style)
k=k+1
#Now sending data to Excel
sql='select * from stud order by admno'
myc.execute(sql)
myresult=myc.fetchall()
row=1
for data in myresult:
column=0
for i in range(len(Field_list)):
if i!=1:
sheet1.write(row,i,data[i],style=style)
else:
sheet1.write(row,i,data[i])
row=row+1
fnm='Student_'+getDate_Time()+str('.xls')
wb.save(fnm)
print('Excel file \"',fnm,'\"created... Pls see your folder')
def main():
a=True
while a:
print('1. Add record ')
print('2. Show record')
print('3. Generate Excel for student record')
print('4. Update record')
print('5. Delete record')
print('6. Exit')
ch=int(input('Enter your choice :'))
if ch==1:
insertRecord()
elif ch==2:
showRecord()
elif ch==3:
genExcel()
elif ch==4:
updateRecord()
elif ch==5:
deleteRecord()
elif ch==6:
a=False
else:
print('Enter proper choice')
#Execution of main program
if __name__=='__main__':
main()