Added create_config_table_string function to clean up the create tables function

This commit is contained in:
Daniel Wilczak
2020-11-16 16:21:56 -05:00
parent 3bf69b21a6
commit 8b6b880ada
2 changed files with 22 additions and 16 deletions

View File

@ -97,7 +97,8 @@ class SQL_Database:
def create_all_tables(self, ga): def create_all_tables(self, ga):
"""Create the data table that store generation data.""" """Create the database if it doenst exist and then the data and config
tables."""
try: try:
# Remove old database file if it exists. # Remove old database file if it exists.
@ -106,28 +107,33 @@ class SQL_Database:
# If the database does not exist continue # If the database does not exist continue
pass pass
# create a database connection # Create the database connection
self.conn = self.create_connection(ga.database_name) self.conn = self.create_connection(ga.database_name)
# create tables
if self.conn is not None: if self.conn is not None:
# Create data table # Create data table
self.create_table(ga.sql_create_data_structure) self.create_table(ga.sql_create_data_structure)
# Creare config table
self.create_table(self.create_config_table_string(ga))
else:
print("Error! cannot create the database connection.")
def create_config_table_string(self,ga):
"""Automate the table creation sql statement so that it takes all the
attribute variables and adds them as columns in the database table config"""
# Retrieve variable names and assign sql data types # Retrieve variable names and assign sql data types
var_names = self.get_var_names(ga) var_names = self.get_var_names(ga)
for i in range(len(var_names)): for i in range(len(var_names)):
var_names[i] += ' ' + self.sql_type_of(var_names[i]) var_names[i] += ' ' + self.sql_type_of(var_names[i])
# Create config table # Structure the config table
sql_create_config_structure = "CREATE TABLE IF NOT EXISTS config (\nid INTEGER PRIMARY KEY," sql = "CREATE TABLE IF NOT EXISTS config (\nid INTEGER PRIMARY KEY,"
sql_create_config_structure += "\n,".join(var_names) sql += "\n,".join(var_names)
sql_create_config_structure += "); " sql += "); "
self.create_table(sql_create_config_structure)
else:
print("Error! cannot create the database connection.")
return sql
def insert_config(self,ga): def insert_config(self,ga):
"""Insert the configuration attributes into the config.""" """Insert the configuration attributes into the config."""

View File

@ -5,7 +5,7 @@ import matplotlib.pyplot as plt
ga = EasyGA.GA() ga = EasyGA.GA()
# Create 25 chromosomes each with 10 genes and 200 generations # Create 25 chromosomes each with 10 genes and 200 generations
ga.population_size = 100 ga.population_size = 200
ga.chromosome_length = 10 ga.chromosome_length = 10
ga.generation_goal = 150 ga.generation_goal = 150