Cleaned up create connection and multiline strings

This commit is contained in:
SimpleArt
2020-12-05 23:41:46 -05:00
parent a6530fb15e
commit d49008c19d

View File

@ -14,6 +14,7 @@ class SQL_Database:
self.config_id = None self.config_id = None
self._database_name = 'database.db' self._database_name = 'database.db'
def default_config_id(method): def default_config_id(method):
"""Decorator used to set the default config_id""" """Decorator used to set the default config_id"""
@ -42,6 +43,7 @@ class SQL_Database:
return new_method return new_method
def get_current_config(self): def get_current_config(self):
"""Get the current config_id from the config table.""" """Get the current config_id from the config table."""
return self.query_one_item("SELECT MAX(id) FROM config") return self.query_one_item("SELECT MAX(id) FROM config")
@ -62,15 +64,12 @@ class SQL_Database:
"""Create a database connection to the SQLite database """Create a database connection to the SQLite database
specified by db_file.""" specified by db_file."""
conn = None
try: try:
conn = sqlite3.connect(self.database_name) self.conn = sqlite3.connect(self.database_name)
return conn
except Error as e: except Error as e:
self.conn = None
print(e) print(e)
self.conn = conn
def create_table(self, create_table_sql): def create_table(self, create_table_sql):
"""Create a table from the create_table_sql statement.""" """Create a table from the create_table_sql statement."""
@ -94,8 +93,10 @@ class SQL_Database:
) )
# Create sql query structure # Create sql query structure
sql = ''' INSERT INTO data(config_id, generation, fitness, chromosome) sql = '''
VALUES(?,?,?,?) ''' INSERT INTO data(config_id, generation, fitness, chromosome)
VALUES(?,?,?,?)
'''
cur = self.conn.cursor() cur = self.conn.cursor()
cur.execute(sql, db_chromosome) cur.execute(sql, db_chromosome)
@ -119,8 +120,10 @@ class SQL_Database:
] ]
# Create sql query structure # Create sql query structure
sql = ''' INSERT INTO data(config_id,generation,fitness,chromosome) sql = '''
VALUES(?,?,?,?) ''' INSERT INTO data(config_id, generation, fitness, chromosome)
VALUES(?,?,?,?)
'''
cur = self.conn.cursor() cur = self.conn.cursor()
cur.executemany(sql, db_chromosome_list) cur.executemany(sql, db_chromosome_list)
@ -142,23 +145,25 @@ class SQL_Database:
"""Create the database if it doenst exist and then the data and config """Create the database if it doenst exist and then the data and config
tables.""" tables."""
# if the database file already exists.
try: try:
# if the database file already exists.
self.config = self.get_current_config() self.config = self.get_current_config()
# If the database does not exist continue
except: except:
# If the database does not exist continue
pass pass
# Create the database connection # Create the database connection
self.conn = self.create_connection() self.create_connection()
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 # Creare config table
self.create_table(self.create_config_table_string(ga)) self.create_table(self.create_config_table_string(ga))
else: else:
raise Exception("Error! cannot create the database connection.") raise Exception("Error! Cannot create the database connection.")
def create_config_table_string(self,ga): def create_config_table_string(self,ga):
@ -300,17 +305,16 @@ class SQL_Database:
if self._conn is not None: if self._conn is not None:
return self._conn return self._conn
else: # If the connection has not been set yet
# If the connection has not been set yet try:
try: # Check if you can connect to the database
# Check if you can connect to the database self.create_connection()
self._conn = self.create_connection() return self._conn
return self._conn
except: # If the connection doesnt exist then print error
# if the connection doesnt exist then print error except:
raise Exception("""You are required to run a ga before you raise Exception("""You are required to run a ga before you
can connect to the database. Run ga.evolve() or ga.active()""") can connect to the database. Run ga.evolve() or ga.active()""")
@conn.setter @conn.setter
@ -329,16 +333,16 @@ class SQL_Database:
if self._config_id is not None: if self._config_id is not None:
return self._config_id return self._config_id
else: # If the config_id has not been set yet
# If the config_id has not been set yet try:
try: # Check if you can connect to the database
# Check if you can connect to the database self._config_id = self.get_most_recent_config_id()
self._config_id = self.get_most_recent_config_id() return self._config_id
except: # If the config_id doesnt exist then print error
# if the config_id doesnt exist then print error except:
raise Exception("""You are required to run a ga before you raise Exception("""You are required to run a ga before you
can connect to the database. Run ga.evolve() or ga.active()""") can connect to the database. Run ga.evolve() or ga.active()""")
@config_id.setter @config_id.setter