Commit d799778b authored by Danielvdh03's avatar Danielvdh03
Browse files

Got insert to work

parent a8e6c12f
......@@ -73,7 +73,7 @@ class Database:
self.command_description = generate_descriptions() # List of user command descriptions
# Returns column names and types for a given table (view_column)
def get_column_names(self, table_name):
def get_column_and_type(self, table_name):
cursor.execute("Show columns from {0}" .format(table_name))
results = cursor.fetchall()
......@@ -85,9 +85,22 @@ class Database:
fields.append(result[0])
type.append(result[1])
print(fields)
# print(fields)
return fields, type
def get_column(self, table_name):
cursor.execute("Show columns from {0}".format(table_name))
results = cursor.fetchall()
fields = [] # field name
for result in results:
fields.append(result[0])
# print(fields)
return fields
# Creates a Table based off user input
def create_table(self):
......@@ -114,7 +127,6 @@ class Database:
attributes = trim(attributes) # Removes white space and unnecessary characters like , ' ( )
print(attributes)
for attribute in attributes: # Gets specifications for each attribute
......@@ -139,7 +151,7 @@ class Database:
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 : \n")
input_pk = input("Enter attributes that you wish to use as Primary Key (separate by commas) : \n")
pk = input_pk.split(",")
......@@ -148,7 +160,9 @@ class Database:
print(query) # For debugging purposes
cursor.execute(query)
results = cursor.fetchAll()
results = cursor.fetchall()
print(results)
self.table_list.append(table_name) # Add to list of tables
# Generates create query based off user information provided
......@@ -178,10 +192,6 @@ class Database:
else:
query +=",\n"
query +=");"
return query
......@@ -264,13 +274,86 @@ class Database:
return pk_string
#Adds rows to a table
def add_records(self):
table_name = input("Enter the table you wish to insert data into : ")
if table_name not in self.table_list:
print("Table {0} does not exist".format(table_name))
return
else:
all = self.get_column(table_name)
print("Attributes are {0}".format(all))
choice = input("Inserting into all columns? (All/Partial) : ")
choice = choice.strip() # Remove whitespace
choice = choice.lower() # Case insensitive
columns = ""
column_names = ""
attributes = []
if choice == "all": # Do nothing
attributes = all
column_names += "\n" # No columns to include
elif choice == "partial": # Specify columns to insert into
names = input("Please enter names of all columns to insert into (seperated by commas) : \n")
columns += names
old_list = names.split(",") # Convert to list
attributes = trim(old_list)
for element in attributes:
if element == attributes[0]:
column_names+="({0},".format(element)
elif element == attributes[-1]:
column_names += "{0})".format(element)
else:
column_names += "{0},".format(element)
else: # Error
print("Error : Invalid input")
return
query = self.generate_insert_query(table_name, column_names, attributes)
cursor.execute(query)
results = cursor.fetchall()
print(results)
# From passed in parameters generates final query
def generate_insert_query(self, table_name, column_names, attributes):
query = "insert into {0}".format(table_name)
query += column_names
query += "values ("
for element in attributes:
value = input("Enter value to be entered into {0} : ".format(element))
if element == attributes[-1]:
query += "\'{0}\');".format(value)
else:
query += "\'{0}\',".format(value)
return query
#Adds rows to a table
def add_records(self):
pass
# Get a number of results that meet criteria based off filtering
def select_count(self):
......@@ -331,7 +414,7 @@ class Database:
if table_name in self.table_list:
field_names, types = self.get_column_names(table_name)
field_names, types = self.get_column_and_type(table_name)
else:
print("Table does not exist")
......@@ -371,6 +454,10 @@ class Database:
print(table)
return
def execute_query(self):
if __name__ == "__main__":
......@@ -423,7 +510,7 @@ if __name__ == "__main__":
client.create_table()
elif command == "insert": # TODO
elif command == "insert": # Done
client.add_records()
......@@ -468,7 +555,6 @@ if __name__ == "__main__":
else:
print("Invalid command, please enter a valid command!")
......
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