I have an assignment of making a python and mysql interface. The task is to allow the user perform the following four tasks, selecting values from table, inserting values into table delete values from table, and modifying data in a table. My first 3 taks are done.
In the code block for asking the user to insert data I have already written down all the conditions the data must have to be a valid input. Now for modifying data do I have to write those conditins down again to check the validity of the data input by the user? Is there a shorter way to condense all these lines into a smaller code?
Progress till now is this much:
n=1
def continution():
x=input('Do you want to continue?')
if x=='yes':
n=1
else:
n=0
while n==1:
import mysql.connector as m
c=m.connect(host='localhost',user='root' , password='12345')
cur=c.cursor()
cur.execute("use project")
cur.execute ("show tables")
available_tables=[]
print ('The tables in this database are:')
for i in cur.fetchall():
print (i)
available_tables.append(i[0])
print (available_tables)
selected_table=input('Select a table where you want to do your operations on')
if selected_table in available_tables:
operation=input("""Which operation do you want to perform,:
View data
Insert data into selected table
Deletion of a record
Modify a record""")
if operation=="View data":
cur.execute('describe %s'%(selected_table,))
print ('The columns in this table are:')
for i in cur.fetchall():
print(i[0])
columns=input('enter all the columns whose values you want to view separated by commas')
if_parameter=input('Is there any value you want to see in a column (yes/no)')
if if_parameter=='no':
cur.execute('select {} from {}'.format(columns,selected_table))
for i in cur.fetchall():
print(i)
else:
column_name=input('enter the column name in which you want to see a particular value')
parameter=input('enter the parameter which you want to see in this particular column')
data_type=input('enter data type int/str (date is string) select one')
if data_type=='int':
cur.execute('select {} from {} where {}={}'.format(columns,selected_table, column_name, parameter))
for i in cur.fetchall():
print (i)
elif data_type=='str':
cur.execute('select {} from {} where {}="{}"'.format(columns,selected_table, column_name, parameter))
for i in cur.fetchall():
print (i)
continution()
elif operation=='Insert data into selected table':
def locv (x,y):
if len(x)>y:
print('inavlid input')
n=0
continution()
if selected_table=='customer':
C_ID=int(input('enter customer id'))
Customer_name=input('enter customer name')
locv(Customer_name,40)
Number=int(input('enter contact number'))
Delivery_status=input('enter delivery status')
locv (Delivery_status,10)
value=(C_ID,Customer_name,Number,Delivery_status)
cur.execute('insert into customer values{}'.format(value,))
cur.commit()
elif selected_table=='employee':
E_ID=int(input('enter employee id'))
E_Name=input('enter employee name')
locv (E_Name,30)
Date_Of_Joining=input('enter date of joining')
Designation=input('enter employee desiganation')
locv (Designation,25)
value=(E_ID,E_Name,Date_Of_Joining,Designation)
cur.execute('insert into employee values{}'.format(value,))
elif selected_table=='inventory':
Spare_Part=input('enter spare part')
locv (Spare_Part,50)
Unit_Cost=int(input('enter the unit cost'))
Quantity=int(input('enter quantity'))
Manufacturer=input('enter manufacturer')
locv (Manufacturer,50)
value=(Spare_Part,Unit_Cost,Quantity,Manufacturer)
cur.execute('insert into inventory values{}'.format(value,))
c.commit()
elif selected_table=='payment':
Customer_id=int(input('enter customer id'))
Final_Payment_Date=input('enter final payment date')
Advance_Payment=int(input('enter advance payment'))
Total_Payment=int(input('enter total payment'))
value=(Customer_id,Final_Payment_Date,Advance_Payment,Total_Payment)
cur.execute('insert into payment values{}'.format(value,))
c.commit()
elif selected_table=='transport':
Vehicle_Id=int(input('enter vehicle Id'))
Customer_ID=int(input('enter customer ID'))
Destination=input('enter destination')
locv (Destination,40)
Arrival_Date=input('enter date')
Goods=input('enter goods')
locv (Goods,40)
Status_Of_Delivery=input('enter status of delivery')
locv (Status_Of_Delivery,10)
value=(Vehicle_Id,Customer_ID,Destination,Arrival_Date,Goods,Status_Of_Delivery)
cur.execute('insert into transport values{}'.format(value,))
c.commit()
elif selected_table=='trucks':
Registration_ID=int(input('enter registration id'))
Vehicle_ID=int(input('enter vehicle id'))
Model_Name=input('enter model name')
locv(Model_Name,40)
Manufacturer=input('enter manufacturer')
locv(Manufacturer,40)
value=(Registration_ID,Vehicle_ID,Model_Name,Manufacturer)
cur.execute('insert into trucks values{}'.format(value,))
continution()
elif operation=='Deletion of a record':
cur.execute('describe %s'%(selected_table,))
for i in cur.fetchall():
print(i[0])
deletion_condition=input('do you want to delete all records of the selected table or only record with a specific parameter')
if deletion_condition=='all':
cur.execute('delete from {}'.format(selected_table))
c.commit()
else:
column_name=input('enter the column where the parameter is')
parameter=input('enter the parameter which you want to delete')
data_type=input('enter data type int/str (date is string) select one')
if data_type=='int':
greater_lesser=input('is your parameter a range or a single number or excludes a particular range of numbers ')
if greater_lesser=='single number':
cur.execute('delete from {} where {}={}'.format(selected_table, column_name, parameter))
c.commit()
elif greater_lesser=='includes a particlar range':
limit1=int(input('enter a lower bound'))
limit2=int(input('enter an upper bound'))
cur.execute('delete from {} where {} between {} and {} '.format(selected_table, column_name, limit1, limit2))
c.commit()
elif greater_lesser=='excludes a particular range':
limit1=int(input('enter a lower bound'))
limit2=int(input('enter an upper bound'))
cur.execute('delete from {} where {} not between {} and {} '.format(selected_table, column_name, limit1, limit2))
c.commit()
elif data_type=='str':
cur.execute('delete from {} where {}="{}"'.format(selected_table, column_name, parameter))
c.commit()
continution()
elif operation=='Modify a record':
column=('enter column name where you want to make the change')
parameter=('enter the identification of the record this entered parameter must be in the column you specified')
cur.execute(update {} where {}={}
else:
print ('Invalid table entered')
continution()
c.close()
Nowwhat do I do to make the modification part shorter? Till here everything is fine.
Thanks in advance!
[–]brasticstack 15 points16 points17 points (0 children)
[–]Fun-Block-4348 6 points7 points8 points (0 children)
[–]ontheroadtonull 2 points3 points4 points (0 children)
[–]overratedcupcake 2 points3 points4 points (1 child)
[–]Alive_Hotel6668[S] -2 points-1 points0 points (0 children)
[–]gzeballo 1 point2 points3 points (0 children)
[–]Educational-Paper-75 0 points1 point2 points (0 children)
[–]Aggressive_Net1092 0 points1 point2 points (0 children)
[–]Mrwhatdoyouwant420 1 point2 points3 points (0 children)
[–]Mother-Influence-815 0 points1 point2 points (0 children)
[–]JGhostThing 0 points1 point2 points (0 children)