I have just started learning databases and started with SQL and I came to know about sqlconnector which would integrate python and sql easily.
So, I am working on a command line program which does all the major tasks done in MySQL but this time without using commands, I added a section in the code which can create a table of any degree, but now I need a section in the code which basically helps to add record in the table as per the number of columns.
Initially I tried to make separate conditions ( for eg- when degree of table is 3, I defined a function named which was explicitly for table of degree 3 and similarly for table of degree 4) , but later I made the table creator code a bit brief and more powerful by using a while loop to put as many number of columns in the table. The thing is, I want the function inputter to be also that much efficient and hence increasing the number of values I can enter in a field.
I have mentioned the code I am talking about. Thanks for your time.
What I have tried:
## to create a table
def table_creator():
table_name = input("PLEASE ENTER THE NAME OF NEW TABLE:")
a = input("NAME FOR COLUMN 1:")
t = input("DATATYPE OF COLUMN 1:")
size = input("SIZE OF COLUMN 1:")
cursor_obj.execute("CREATE TABLE {} ({} {}({}))".format(table_name,a,t,size))
x = int(input("PLEASE ENTER THE NUMBER OF COLUMNS YOU WANT IN TABLE:"))
i = 1
while i < x:
a1 = input("NAME FOR COLUMN {}:".format(i+1))
t1 = input("DATATYPE FOR COLUMN {}:".format(i+1))
size1 = input("SIZE OF COLUMN {}:".format(i+1))
cursor_obj.execute('Alter table {} add column {} {}({})'.format(table_name,a1,t1,size1))
def inputer_count3():
#taking records from user to enter in the table
table_name = input("Please enter the table to insert value")
a = input(" Do you want to see the table structure: ")
if a == 'yes' or 'y':
cursor_obj.execute("describe {}".format(table_name))
for x in cursor_obj:
print(x)
elif a == 'no' or 'n':
print("ok")
else:
print("INVALID SELECTION enter 'y' or 'yes for yes and 'n' or 'no for no")
value1 = input("please enter the value for column 1:")
value2 = input("please enter the value for column 2:")
value3 = input("please enter the value for column 3:")
#Entering values into the table
cursor_obj.execute('insert into {} values("{}",{},{})'.format(table_name,value1,value2,value3))
def inputer_count4():
#taking records from user to enter in the table
table_name = input("Please enter the name of the table :")
a = input("Do you want to see the table structure? ")
if a == 'yes' or 'y':
cursor_obj.execute("describe table {}".format(table_name))
for i in cursor_obj:
print(i)
elif a == 'no' or 'n':
print("ok")
else:
print("INVALID SELECTION enter 'y' or 'yes for yes and 'n' or 'no for no")
value1 = input("Please enter the value for column 1:")
value2 = input("Please enter the value for column 2:")
value3 = input("Please enter the value for column 3:")
value4 = input("Please enter the value for column 4:")
#Entering values into the table
cursor_obj.execute('insert into {} values("{}",{},{},{})'.format(table_name,value1,value2,value3,value4))
elif choice == 'd':
cursor_obj.execute("use practice")
table_name = input("PLEASE ENTER THE NAME OF THE TABLE: ")
cursor_obj.execute("set @a = (select count(*) as count from information_schema.columns where table_name = '{}') ".format(table_name))
cursor_obj.execute("select @a")
for i in cursor_obj:
x = i
y = x[0]
if y == 3:
inputer_count3()
elif y == 4:
inputer_count4()
else:
print("Invalid selection, please redo the actions")