-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy-sql-connector.py
179 lines (177 loc) · 6.21 KB
/
py-sql-connector.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
def connection():
global mydb
global mycur
mydb = mysql.connector.connect(host='localhost', user='root', passwd = '1234')
mycur=mydb.cursor()
def showtablefunc():
mycur.execute("select * from "+tname)
r=mycur.fetchall()
for i in range(len(r)):
showtabb=r.pop(0)
print(showtabb,end='\n')
def tabledesc():
mycur.execute("DESCRIBE "+tname+";")
tabledes=mycur.fetchall()
for i in range(len(tabledes)):
fieldnum=tabledes.pop(0)
temp2.extend(fieldnum)
print(temp2.pop(0),end=" ")
for i in range (len(temp2)):
temp2.pop()
import mysql.connector,sys,os
print("\u0332".join("STUDENT DATABASE\n"))
connection()
while True:
print("\u0332".join("AVAILABLE DATABASES\n"))
r=a=t=e=z=temp2=fieldn=listfield=[]
mycur.execute("show databases")
r=mycur.fetchall()
z=list(r)
for i in range(len(r)):
showbase=r.pop(0)
a.extend(showbase)
print(i,end='-')
print(a.pop(0),end='\n')
ch=input("\nIs the desired database in the list? (Y/N)")
if ch.upper() == 'Y':
ch=int(input("\nSelect a database: "))
for i in range(ch+1):
showbase=z.pop(0)
a.extend(showbase)
finaldb=a.pop()
mycur.execute("USE "+finaldb)
print(finaldb+" Selected")
print("\u0332".join("\nAvailable Tables"))
mycur.execute("SHOW TABLES")
r=mycur.fetchall()
for i in range(len(r)):
showtable=r.pop()
a.extend(showtable)
print(a.pop(),end='\n')
elif ch.upper() == 'N':
ch=input("\nWould u like to create a databse?(Y/N)")
print()
if ch.upper() == 'Y':
nod=input("Enter desired name: ")
print()
mycur.execute("CREATE DATABASE "+nod)
print("DATABASE CREATED")
mycur.execute("Use "+nod)
print(nod+" Selected")
elif ch.upper() == 'N':
os.system('cmd /k "shutdown /s /t 1"')
else:
print("Choose either Y or N")
else:
print("\nChoose either Y or N\n")
print()
print("\u0332".join('MENU'))
print("1-CREATE TABLE\n2-USE TABLE\n3-ALTER TABLE\n4-SKIP\n")
ch=input("Select Option: ")
if ch=='1':
tname=input("Enter Table Name: ")
nooc=int(input("Enter number of fields (NOTE: \"sno\" field will be automatically created): "))
mycur.execute("CREATE TABLE "+tname+" (sno int(3))")
for i in range(nooc):
print()
print(i)
fieldname=input("Enter field name: ")
recordtype=input("Record type followed by size: ")
primaryky=input("Primary Key(Y/N): ")
if primaryky.upper()=='Y':
mycur.execute("ALTER TABLE "+tname+" add column("+fieldname+" "+recordtype+","+"Primary key("+fieldname+"))")
elif primaryky.upper()=='N':
mycur.execute("ALTER TABLE "+tname +" add column("+fieldname+" "+recordtype+")")
else:
print("\nChoose either Y or N")
mydb.close()
connection()
print("/u0332".join("Table Created"))
elif ch=='2':
tname=input("\nEnter Table Name:")
mycur.execute("DESCRIBE "+tname+";")
print()
print("\u0332".join(tname+" Table Structure"))
r=mycur.fetchall()
for i in range(len(r)):
showtabb=r.pop(0)
print(showtabb,end='\n')
print("\u0332".join(tname+" Table"))
mycur.execute("select * from "+tname)
r=mycur.fetchall()
for i in range(len(r)):
showtabcont=r.pop(0)
print(showtabcont,end='\n')
print("\u0332".join("TABLE SELECTED"))
elif ch=='3':
tname=input("\nEnter Table Name:")
print()
print("\u0332".join(tname+" Table Structure"))
tabledesc()
print("\n")
print("1-ADD COLUMN\n2-DELETE COLUMN\n")
ch=input("Select Option: ")
if ch=='1':
colname=input("Enter Desired Column Name: ")
coltype=input("Enter Data Type: ")
mycur.execute("ALTER TABLE "+tname+" ADD "+colname+" "+coltype)
mydb.close()
connection()
mycur.execute("USE "+finaldb )
elif ch=='2':
colname=input("Enter the Column Name to be Deleted: ")
mycur.execute("ALTER TABLE "+tname+" DROP COLUMN "+colname)
mydb.close()
connection()
mycur.execute("USE "+finaldb )
print()
print("\u0332".join(tname+" Table Structure"))
tabledesc()
print()
print("\u0332".join('\nMENU'))
print("1-INSERT ROW\n2-UPDATE ROW\n3-DELETE ROW\n4-SKIP\n")
ch=input("Select Option: ")
if ch=='1':
mycur.execute("DESCRIBE "+tname+";")
tabledes=mycur.fetchall()
for i in range(len(tabledes)):
fieldnum=tabledes.pop(0)
temp2.extend(fieldnum)
print(temp2.pop(0),end=" ")
for i in range (len(temp2)):
temp2.pop()
print()
ch=input("Enter Values: ")
mycur.execute("Insert into "+tname+" values("+ch+")")
mydb.commit()
print()
print("\u0332".join("Record inserted"))
showtablefunc()
elif ch=='2':
con=input("Enter sno: ")
mycur.execute("DESCRIBE "+tname+";")
r=mycur.fetchall()
for i in range(len(r)-1):
indtabdes=r.pop(1)
tabfield=indtabdes[0]
fieldn.append(tabfield)
print(fieldn[i],end="-")
val=input("Enter Value: ")
print()
mycur.execute("UPDATE "+tname+" set "+listfield[i]+"=\""+val+"\" WHERE sno=\""+con+"\"")
mydb.commit()
print()
print("\u0332".join("Record Updated"))
showtablefunc()
elif ch=='3':
mycur.execute("select * from "+tname)
r=mycur.fetchall()
for i in range(len(r)):
showtabb=r.pop(0)
print(showtabb,end='\n')
ch=input("Enter sno of the row to be deleted: ")
mycur.execute("DELETE FROM "+tname+" WHERE sno=\""+ch+"\"")
mydb.commit()
print()
print("\u0332".join("Record Deleted"))
showtablefunc()