Commit 85959caa authored by Danielvdh03's avatar Danielvdh03
Browse files

Refactored Create to make UI more tolerable

parent ccca83a5
......@@ -89,7 +89,6 @@ class Database:
else:
executing = True
attributes = []
type = []
max_length = []
......@@ -97,69 +96,56 @@ class Database:
null = []
pk = []
while executing:
print ("Make sure the fields you entered below are seperated by commas (ex. \"first_name, last_name, age\")")
var = input("Continue inputting attributes into {0} (Y/N) : ".format(table_name))
input_string = input("Enter all fields that you want in table {0} seperated by commas : \n".format(table_name) )
attributes = input_string.split(",")
if var == "N" and len(attributes) < 2: # Not enough attributes
new_list = []
print("Only {0} attributes. Need at least 2 attributes to create table.".format(len(attributes)))
# Removes unnecessary leading or ending whitespaces
elif var == "N": # Enough attributes leaving....
for attribute in attributes:
break
attribute = attribute.strip()
new_list.append(attribute)
elif var == "Y":
attributes = new_list
new_attribute = input("Enter the name of the next attribute to add : ")
print(attributes)
new_type = input("Enter the type of attribute {0} (Integer, Decimal, Date, Character, Boolean) : ".format(new_attribute))
new_type = new_type.lower() #No longer case sensitive
for attribute in attributes: # Gets specifications for each attribute
new_type, new_length, new_floating = self.handle_type_insert(new_type)
attributes.append(new_attribute)
type.append(new_type)
max_length.append(new_length)
num_floating.append(new_floating)
new_type = input("Enter the type of attribute {0} (Integer, Decimal, Date, Character, Boolean) : ".format(attribute))
new_type = new_type.lower() #No longer case sensitive
new_null = input("Does this attribute always have to be defined (Y/N) : ")
if new_null == "Y":
null.append("not null")
else:
null.append("")
new_type, new_length, new_floating = self.handle_type_insert(new_type)
type.append(new_type)
max_length.append(new_length)
num_floating.append(new_floating)
new_null = input("Does this attribute always have to be defined (Y/N) : ")
if new_null == "Y":
null.append("not null")
else:
null.append("")
print("Invalid command. Make sure to enter either \"Y\" or \"N\"")
no_pk = True
while no_pk:
user = input("Do you want to add a Primary Key to the table (Y/N)")
user = input("Do you want to add a Primary Key to the table (Y/N)")
if user == "Y":
if user == "Y":
print("Attributes created are : {0}".format(attributes))
input_pk = input("Enter attributes that you wish to use as Primary Key (separate by commas and no spaces) : \n")
variable = input("Enter attribute to append to PK : ")
pk = input_pk.split(",")
if variable in attributes:
pk.append(variable)
else:
print("Error attribute {0} does not exist.")
print("Attributes that exist are {0}".format(attributes))
else:
break
self.generate_create_query(table_name, attributes, type, max_length, num_floating, null, pk)
query = self.generate_create_query(table_name, attributes, type, max_length, num_floating, null, pk)
print(query)
cursor.execute(query)
results = cursor.fetchAll()
print(results)
# Generates create query based off user information provided
......@@ -178,11 +164,7 @@ class Database:
if attribute == attributes[-1]:
if len(pk)==0:
break
else:
if len(pk)!=0:
query +=",\n"
pk_string = self.handle_pk_string(pk)
......@@ -198,8 +180,7 @@ class Database:
query +=");"
print(query)
return query
# Returns datatype for query
def format_type(self, type, max_length, num_floating):
......@@ -264,7 +245,7 @@ class Database:
return "error", -1, -1
def handle_PK_query(self, pk):
def handle_pk_string(self, pk):
pk_string = ""
......@@ -276,6 +257,8 @@ class Database:
else:
pk_string += "{0} ,".format(element)
return pk_string
......@@ -371,13 +354,6 @@ if __name__ == "__main__":
dbname = os.environ.get("DB_NAME")
keepExecuting = True
if username == "{Username}":
print ("looks like the username is still the default on the .env file.")
elif passphrase == "{Password}":
print ("Looks like the password is still the default on the .env file")
try:
db = mysql.connect(
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment