diff --git a/Database/__pycache__/id.cpython-311.pyc b/Database/__pycache__/id.cpython-311.pyc new file mode 100644 index 00000000..0a8e97bd Binary files /dev/null and b/Database/__pycache__/id.cpython-311.pyc differ diff --git a/Database/createTables.py b/Database/createTables.py index c9460dae..1ec4dcfa 100644 --- a/Database/createTables.py +++ b/Database/createTables.py @@ -1,60 +1,51 @@ import sqlite3 +import os +import csv + + +# Function to determine the data type of a value +def get_data_type(value): + try: + int(value) + return "INTEGER" + except ValueError: + try: + float(value) + return "REAL" + except ValueError: + return "TEXT" + # Connect to the SQLite database conn = sqlite3.connect("items.db") cursor = conn.cursor() -# Define the table schema for reloading -cursor.execute( - """ - CREATE TABLE IF NOT EXISTS reloading ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - type TEXT, - name TEXT, - rarity INTEGER, - weight REAL, - width INTEGER, - height INTEGER, - stack INTEGER, - value INTEGER - ) -""" -) +# Get a list of CSV files in the "data" directory +data_dir = "data" # Change this to your data directory path +csv_files = [f for f in os.listdir(data_dir) if f.endswith(".csv")] -# Define the table schema for ammo -cursor.execute( - """ - CREATE TABLE IF NOT EXISTS ammo ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - type TEXT, - name TEXT, - rarity INTEGER, - weight REAL, - width INTEGER, - height INTEGER, - stack INTEGER, - value INTEGER, - gunpowder INTEGER - ) -""" -) +# Iterate through CSV files and create tables +for csv_file in csv_files: + table_name = os.path.splitext(csv_file)[ + 0 + ] # Remove the file extension to get the table name -# Define the table schema for ammo -cursor.execute( - """ - CREATE TABLE IF NOT EXISTS craft ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - type TEXT, - name TEXT, - rarity INTEGER, - weight REAL, - width INTEGER, - height INTEGER, - stack INTEGER, - value INTEGER - ) -""" -) + # Read the first row of the CSV file to determine the column names and data types + with open(os.path.join(data_dir, csv_file), newline="") as csvfile: + csv_reader = csv.reader(csvfile) + header = next(csv_reader) -# Close the connection + data_types = [get_data_type(value) for value in header] + + # Generate the CREATE TABLE statement dynamically based on the column names and data types + create_table_sql = f"CREATE TABLE IF NOT EXISTS {table_name} (\n" + for column_name, data_type in zip(header, data_types): + create_table_sql += f" {column_name} {data_type},\n" + create_table_sql = create_table_sql.rstrip(",\n") + "\n);" + + # Execute the CREATE TABLE statement + cursor.execute(create_table_sql) + +# Commit the changes and close the connection +conn.commit() conn.close() diff --git a/Database/data/ammo.csv b/Database/data/ammo.csv index a6465978..211711ed 100644 --- a/Database/data/ammo.csv +++ b/Database/data/ammo.csv @@ -1,14 +1,13 @@ id,type,name,rarity,weight,width,height,stack,value,gunpowder -NULL,rifle_ammo,5.56x45 AP,4,0.02646,1,1,30,7500,15 -NULL,rifle_ammo,5.56x45 FMJ,1,0.02646,1,1,30,2500,10 -NULL,rifle_ammo,5.56x45 HP,2,0.02646,1,1,30,5000,10 -NULL,pistol_ammo,9mm AP,3,0.01984,1,1,45,5000,12 -NULL,pistol_ammo,9mm FMJ,1,0.01984,1,1,45,2000,6 -NULL,pistol_ammo,9mm HP,2,0.01984,1,1,45,4000,6 -NULL,pistol_ammo,.40 S&W AP,3,0.01984,1,1,45,5500,13 -NULL,pistol_ammo,.40 S&W FMJ,1,0.01984,1,1,45,3000,7 -NULL,pistol_ammo,.40 S&W HP,2,0.01984,1,1,45,4500,7 -NULL,rifle_ammo,7.62x39 AP,4,0.02646,1,1,30,5200,15 -NULL,rifle_ammo,7.62x39 FMJ,1,0.02646,1,1,30,1800,10 -NULL,rifle_ammo,7.62x39 HP,2,0.02646,1,1,30,3600,10 -NULL,rifle_ammo,27.62x39 HP,2,0.02646,1,1,30,3600,10 +NULL,ammoRifle,5.56x45 AP,4,0.02646,1,1,30,7500,15 +NULL,ammoRifle,5.56x45 FMJ,1,0.02646,1,1,30,2500,10 +NULL,ammoRifle,5.56x45 HP,2,0.02646,1,1,30,5000,10 +NULL,ammoPistol,9mm AP,3,0.01984,1,1,45,5000,12 +NULL,ammoPistol,9mm FMJ,1,0.01984,1,1,45,2000,6 +NULL,ammoPistol,9mm HP,2,0.01984,1,1,45,4000,6 +NULL,ammoPistol,.40 S&W AP,3,0.01984,1,1,45,5500,13 +NULL,ammoPistol,.40 S&W FMJ,1,0.01984,1,1,45,3000,7 +NULL,ammoPistol,.40 S&W HP,2,0.01984,1,1,45,4500,7 +NULL,ammoRifle,7.62x39 AP,4,0.02646,1,1,30,5200,15 +NULL,ammoRifle,7.62x39 FMJ,1,0.02646,1,1,30,1800,10 +NULL,ammoRifle,7.62x39 HP,2,0.02646,1,1,30,3600,10 \ No newline at end of file diff --git a/Database/data/craft.csv b/Database/data/craft.csv index ef5580af..ad6e0858 100644 --- a/Database/data/craft.csv +++ b/Database/data/craft.csv @@ -1,52 +1,52 @@ id,type,name,rarity,weight,width,height,stack,value -NULL,Basic Materials,Coal,2,0.02100,1,1,5,2500 -NULL,Basic Materials,Wood,1,0.02100,1,1,20,1000 -NULL,Basic Materials,Leather,2,0.25000,1,1,2,20000 -NULL,Basic Materials,Cloth,1,0.03086,1,1,5,5000 -NULL,Basic Materials,Plastic,1,0.22046,1,1,10,600 -NULL,Basic Materials,Thread,1,0.00772,1,1,20,250 -NULL,Basic Materials,Acid,1,0.02100,1,1,10,2500 -NULL,Basic Materials,Adhesive,2,0.03969,1,1,10,6000 -NULL,Basic Materials,Antiseptic,2,0.03969,1,1,2,5000 -NULL,Basic Materials,Oil,2,0.06857,1,1,10,5000 -NULL,Basic Materials,Steel,1,0.06857,1,1,20,300 -NULL,Basic Materials,Copper,2,0.06857,1,1,10,2000 -NULL,Basic Materials,Zinc,1,0.06857,1,1,10,400 -NULL,Basic Materials,Lead,2,0.34286,1,1,5,1500 -NULL,Basic Materials,Aluminum,2,0.06857,1,1,10,1200 -NULL,Basic Materials,Gold Scrap,5,0.06857,1,1,10,250000 -NULL,Basic Materials,Silver Scrap,4,0.06857,1,1,10,50000 -NULL,Basic Materials,Glass,1,0.03969,1,1,10,1000 -NULL,Basic Materials,Ceramic,2,0.22046,1,1,10,1000 -NULL,Tools,Hammer,1,1.25000,1,2,1,75000 -NULL,Tools,Flathead Screwdriver,1,0.33069,1,1,2,25000 -NULL,Tools,Phillip Head Screwdriver,1,0.33069,1,1,2,25000 -NULL,Tools,Set of Wrenches,3,3.00000,2,2,4,165000 -NULL,Tools,Large Pliers,1,0.33069,1,1,1,25000 -NULL,Tools,Needle Nose Pliers,1,0.33069,1,1,1,25000 -NULL,Tools,Measuring Tape,1,0.33069,1,1,1,40000 -NULL,Tools,Lock Pick,2,0.05000,1,1,5,50000 -NULL,Tools,Drill,3,4.00000,2,2,4,225000 -NULL,Tools,Multi-Tool,4,0.25000,1,1,1,400000 -NULL,Tools,Roll Pin Punch Set,3,3.00000,2,2,4,120000 -NULL,Tools,Set of Files,3,2.00000,2,2,4,200000 -NULL,Tools,Rotary Tool,3,1.50000,1,2,2,180000 -NULL,Tools,Bench Vise,3,30.00000,2,2,4,350000 -NULL,Tools,Scissors,1,0.20000,2,1,2,35000 -NULL,Tools,Set of sockets,3,3.12500,2,1,2,50000 -NULL,Tools,Socket,1,0.31250,1,1,10,5000 -NULL,Tools,Wrench,1,0.60000,1,2,2,33000 -NULL,Tools,File,1,0.40000,1,2,2,40000 -NULL,Tools,Ratchet,2,1.00000,1,2,2,25000 -NULL,Tools,Needle,1,0.01544,1,1,10,200 -NULL,Advanced Materials,Brass,3,0.06857,1,1,10,2500 -NULL,Advanced Materials,Tape,2,0.11023,1,1,1,5000 -NULL,Advanced Materials,Ballistic Fiber,3,0.57500,1,1,2,25000 -NULL,Advanced Materials,Circuitry,3,0.22046,1,1,10,20000 -NULL,Advanced Materials,Springs,3,0.07938,1,1,5,10000 -NULL,Advanced Materials,Screws,3,0.07938,1,1,10,5000 -NULL,Advanced Materials,Gears,3,0.07938,1,1,5,8000 -NULL,Advanced Materials,Rubber,1,0.22046,1,1,10,5600 -NULL,Junk,Car Battery,3,35.00000,3,2,6,500000 -NULL,Junk,Pre-War Money,3,0.00050,1,1,100,5000 -NULL,Junk,Syringe,2,0.22046,1,1,1,7500 \ No newline at end of file +NULL,matBasic,Coal,2,0.021,1,1,5,2500 +NULL,matBasic,Wood,1,0.021,1,1,20,1000 +NULL,matBasic,Leather,2,0.25,1,1,2,20000 +NULL,matBasic,Cloth,1,0.03086,1,1,5,5000 +NULL,matBasic,Plastic,1,0.22046,1,1,10,600 +NULL,matBasic,Thread,1,0.00772,1,1,20,250 +NULL,matBasic,Acid,1,0.021,1,1,10,2500 +NULL,matBasic,Adhesive,2,0.03969,1,1,10,6000 +NULL,matBasic,Antiseptic,2,0.03969,1,1,2,5000 +NULL,matBasic,Oil,2,0.06857,1,1,10,5000 +NULL,matBasic,Steel,1,0.06857,1,1,20,300 +NULL,matBasic,Copper,2,0.06857,1,1,10,2000 +NULL,matBasic,Zinc,1,0.06857,1,1,10,400 +NULL,matBasic,Lead,2,0.34286,1,1,5,1500 +NULL,matBasic,Aluminum,2,0.06857,1,1,10,1200 +NULL,matBasic,Gold Scrap,5,0.06857,1,1,10,250000 +NULL,matBasic,Silver Scrap,4,0.06857,1,1,10,50000 +NULL,matBasic,Glass,1,0.03969,1,1,10,1000 +NULL,matBasic,Ceramic,2,0.22046,1,1,10,1000 +NULL,tool,Hammer,1,1.25,1,2,1,75000 +NULL,tool,Flathead Screwdriver,1,0.33069,1,1,2,25000 +NULL,tool,Phillip Head Screwdriver,1,0.33069,1,1,2,25000 +NULL,tool,Set of Wrenches,3,3,2,2,4,165000 +NULL,tool,Large Pliers,1,0.33069,1,1,1,25000 +NULL,tool,Needle Nose Pliers,1,0.33069,1,1,1,25000 +NULL,tool,Measuring Tape,1,0.33069,1,1,1,40000 +NULL,tool,Lock Pick,2,0.05,1,1,5,50000 +NULL,tool,Drill,3,4,2,2,4,225000 +NULL,tool,Multi-Tool,4,0.25,1,1,1,400000 +NULL,tool,Roll Pin Punch Set,3,3,2,2,4,120000 +NULL,tool,Set of Files,3,2,2,2,4,200000 +NULL,tool,Rotary Tool,3,1.5,1,2,2,180000 +NULL,tool,Bench Vise,3,30,2,2,4,350000 +NULL,tool,Scissors,1,0.2,2,1,2,35000 +NULL,tool,Set of sockets,3,3.125,2,1,2,50000 +NULL,tool,Socket,1,0.3125,1,1,10,5000 +NULL,tool,Wrench,1,0.6,1,2,2,33000 +NULL,tool,File,1,0.4,1,2,2,40000 +NULL,tool,Ratchet,2,1,1,2,2,25000 +NULL,tool,Needle,1,0.01544,1,1,10,200 +NULL,matAdv,Brass,3,0.06857,1,1,10,2500 +NULL,matAdv,Tape,2,0.11023,1,1,1,5000 +NULL,matAdv,Ballistic Fiber,3,0.575,1,1,2,25000 +NULL,matAdv,Circuitry,3,0.22046,1,1,10,20000 +NULL,matAdv,Springs,3,0.07938,1,1,5,10000 +NULL,matAdv,Screws,3,0.07938,1,1,10,5000 +NULL,matAdv,Gears,3,0.07938,1,1,5,8000 +NULL,matAdv,Rubber,1,0.22046,1,1,10,5600 +NULL,junk,Car Battery,3,35,3,2,6,500000 +NULL,junk,Pre-War Money,3,0.0005,1,1,100,5000 +NULL,junk,Syringe,2,0.22046,1,1,1,7500 diff --git a/Database/id.py b/Database/id.py new file mode 100644 index 00000000..404e98dd --- /dev/null +++ b/Database/id.py @@ -0,0 +1,7 @@ +# starting_ids.py + +starting_ids = { + "reloading": 20000, + "ammo": 10000, + "craft": 30000 + } diff --git a/Database/importData.py b/Database/importData.py index b27e999a..201785dc 100644 --- a/Database/importData.py +++ b/Database/importData.py @@ -1,6 +1,7 @@ import csv import os import sqlite3 +from id import starting_ids # Connect to the SQLite database conn = sqlite3.connect("items.db") @@ -9,41 +10,12 @@ cursor = conn.cursor() # Define the directory where the CSV files are located csv_directory = "data" # Change this to your directory path -# Define the CSV file names -csv_file_reloading = "reloading.csv" -csv_file_ammo = "ammo.csv" -csv_file_craft = "craft.csv" - -# Build the full paths to the CSV files -csv_path_reloading = os.path.join(csv_directory, csv_file_reloading) -csv_path_ammo = os.path.join(csv_directory, csv_file_ammo) -csv_path_craft = os.path.join(csv_directory, csv_file_craft) - -# Define the starting ID values for each table -starting_id_reloading = 20000 -starting_id_ammo = 10000 -starting_id_craft = 30000 - -# Set the starting ID values for each table using INSERT statements -cursor.execute(f"INSERT INTO reloading (id) VALUES ({starting_id_reloading})") -cursor.execute(f"INSERT INTO ammo (id) VALUES ({starting_id_ammo})") -cursor.execute(f"INSERT INTO craft (id) VALUES ({starting_id_craft})") - -# Reset the ID sequences for all tables -cursor.execute("DELETE FROM SQLITE_SEQUENCE") -cursor.execute( - f"INSERT INTO SQLITE_SEQUENCE (name, seq) VALUES ('reloading', {starting_id_reloading})" -) -cursor.execute( - f"INSERT INTO SQLITE_SEQUENCE (name, seq) VALUES ('ammo', {starting_id_ammo})" -) -cursor.execute( - f"INSERT INTO SQLITE_SEQUENCE (name, seq) VALUES ('craft', {starting_id_craft})" -) +# Define the starting IDs for each table type +# starting_ids = {"reloading": 20000, "ammo": 10000, "craft": 30000} # Function to load data from a CSV file into a table -def load_csv_data(csv_path, table_name, cursor): +def load_csv_data(csv_path, table_name, cursor, starting_id): # Delete existing data in the table delete_query = f"DELETE FROM {table_name}" cursor.execute(delete_query) @@ -55,18 +27,23 @@ def load_csv_data(csv_path, table_name, cursor): # Exclude the first column (id) from the row values = row[1:] placeholders = ", ".join(["?"] * len(values)) - insert_query = f"INSERT INTO {table_name} VALUES (NULL, {placeholders})" - cursor.execute(insert_query, values) + insert_query = f"INSERT INTO {table_name} VALUES (?, {placeholders})" + cursor.execute(insert_query, [starting_id] + values) + starting_id += 1 -# Load data from the reloading CSV file into the reloading table -load_csv_data(csv_path_reloading, "reloading", cursor) +# Get a list of CSV files in the data directory +csv_files = [f for f in os.listdir(csv_directory) if f.endswith(".csv")] -# Load data from the ammo CSV file into the ammo table -load_csv_data(csv_path_ammo, "ammo", cursor) - -# Load data from the ammo CSV file into the ammo table -load_csv_data(csv_path_craft, "craft", cursor) +# Loop through the CSV files and load data into respective tables +for csv_file in csv_files: + table_name = os.path.splitext(csv_file)[ + 0 + ] # Remove the file extension to get the table name + csv_path = os.path.join(csv_directory, csv_file) + if table_name in starting_ids: + starting_id = starting_ids[table_name] + load_csv_data(csv_path, table_name, cursor, starting_id) # Commit the changes and close the connection conn.commit() diff --git a/Database/items.db b/Database/items.db index 9af55b2b..e35dcfec 100644 Binary files a/Database/items.db and b/Database/items.db differ