import sqlite3 class DatabaseOperations: def init_database(self): # Create or open the file-based database disk_conn = sqlite3.connect('pokemon_forms.db') disk_cursor = disk_conn.cursor() # Create tables in the file-based database self.create_pokemon_forms_table(disk_cursor) self.create_pokemon_storage_table(disk_cursor) self.create_evolution_chains_table(disk_cursor) self.create_exclusive_encounter_groups_table(disk_cursor) self.create_encounters_table(disk_cursor) # Commit changes to the file-based database disk_conn.commit() # Copy the file-based database to the in-memory database disk_conn.backup(self.conn) # Close the file-based database connection disk_conn.close() # Create tables in the in-memory database (in case they weren't copied) self.create_pokemon_forms_table(self.cursor) self.create_pokemon_storage_table(self.cursor) self.create_evolution_chains_table(self.cursor) self.create_exclusive_encounter_groups_table(self.cursor) self.create_encounters_table(self.cursor) # Commit changes to the in-memory database self.conn.commit() def create_pokemon_forms_table(self, cursor): cursor.execute(''' CREATE TABLE IF NOT EXISTS pokemon_forms ( PFIC TEXT PRIMARY KEY, name TEXT NOT NULL, form_name TEXT, national_dex INTEGER NOT NULL, generation INTEGER NOT NULL ) ''') def create_pokemon_storage_table(self, cursor): cursor.execute(''' CREATE TABLE IF NOT EXISTS pokemon_storage ( PFIC TEXT PRIMARY KEY, storable_in_home BOOLEAN NOT NULL, FOREIGN KEY (PFIC) REFERENCES pokemon_forms (PFIC) ) ''') def create_evolution_chains_table(self, cursor): cursor.execute(''' CREATE TABLE IF NOT EXISTS evolution_chains ( from_pfic TEXT, to_pfic TEXT, method TEXT, PRIMARY KEY (from_pfic, to_pfic), FOREIGN KEY (from_pfic) REFERENCES pokemon_forms (PFIC), FOREIGN KEY (to_pfic) REFERENCES pokemon_forms (PFIC) ) ''') def create_exclusive_encounter_groups_table(self, cursor): cursor.execute(''' CREATE TABLE IF NOT EXISTS exclusive_encounter_groups ( id INTEGER PRIMARY KEY AUTOINCREMENT, group_name TEXT NOT NULL, description TEXT ) ''') def create_encounters_table(self, cursor): # First, check if the table exists cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='encounters'") table_exists = cursor.fetchone() is not None if not table_exists: # If the table doesn't exist, create it with all columns cursor.execute(''' CREATE TABLE encounters ( id INTEGER PRIMARY KEY AUTOINCREMENT, pfic TEXT, game TEXT, location TEXT, day TEXT, time TEXT, dual_slot TEXT, static_encounter BOOLEAN, static_encounter_count INTEGER, extra_text TEXT, stars TEXT, fishing BOOLEAN, rods TEXT, exclusive_group_id INTEGER, FOREIGN KEY (pfic) REFERENCES pokemon_forms (PFIC), FOREIGN KEY (exclusive_group_id) REFERENCES exclusive_encounter_groups (id) ) ''') else: # If the table exists, check if the exclusive_group_id column exists cursor.execute("PRAGMA table_info(encounters)") columns = [column[1] for column in cursor.fetchall()] if 'exclusive_group_id' not in columns: # If the column doesn't exist, add it cursor.execute(''' ALTER TABLE encounters ADD COLUMN exclusive_group_id INTEGER REFERENCES exclusive_encounter_groups (id) ''')