just add csv and run main.py, set id starts in id.py

This commit is contained in:
0ceanSlim 2023-09-04 20:32:51 -04:00
parent 69aa59f9ce
commit ac1c9bd30e
7 changed files with 129 additions and 155 deletions

Binary file not shown.

View File

@ -1,60 +1,51 @@
import sqlite3 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 # Connect to the SQLite database
conn = sqlite3.connect("items.db") conn = sqlite3.connect("items.db")
cursor = conn.cursor() cursor = conn.cursor()
# Define the table schema for reloading # Get a list of CSV files in the "data" directory
cursor.execute( data_dir = "data" # Change this to your data directory path
""" csv_files = [f for f in os.listdir(data_dir) if f.endswith(".csv")]
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
)
"""
)
# Define the table schema for ammo # Iterate through CSV files and create tables
cursor.execute( for csv_file in csv_files:
""" table_name = os.path.splitext(csv_file)[
CREATE TABLE IF NOT EXISTS ammo ( 0
id INTEGER PRIMARY KEY AUTOINCREMENT, ] # Remove the file extension to get the table name
type TEXT,
name TEXT,
rarity INTEGER,
weight REAL,
width INTEGER,
height INTEGER,
stack INTEGER,
value INTEGER,
gunpowder INTEGER
)
"""
)
# Define the table schema for ammo # Read the first row of the CSV file to determine the column names and data types
cursor.execute( with open(os.path.join(data_dir, csv_file), newline="") as csvfile:
""" csv_reader = csv.reader(csvfile)
CREATE TABLE IF NOT EXISTS craft ( header = next(csv_reader)
id INTEGER PRIMARY KEY AUTOINCREMENT,
type TEXT,
name TEXT,
rarity INTEGER,
weight REAL,
width INTEGER,
height INTEGER,
stack INTEGER,
value INTEGER
)
"""
)
# 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() conn.close()

View File

@ -1,14 +1,13 @@
id,type,name,rarity,weight,width,height,stack,value,gunpowder 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,ammoRifle,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,ammoRifle,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,ammoRifle,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,ammoPistol,9mm AP,3,0.01984,1,1,45,5000,12
NULL,pistol_ammo,9mm FMJ,1,0.01984,1,1,45,2000,6 NULL,ammoPistol,9mm FMJ,1,0.01984,1,1,45,2000,6
NULL,pistol_ammo,9mm HP,2,0.01984,1,1,45,4000,6 NULL,ammoPistol,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,ammoPistol,.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,ammoPistol,.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,ammoPistol,.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,ammoRifle,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,ammoRifle,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,ammoRifle,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
1 id type name rarity weight width height stack value gunpowder
2 NULL rifle_ammo ammoRifle 5.56x45 AP 4 0.02646 1 1 30 7500 15
3 NULL rifle_ammo ammoRifle 5.56x45 FMJ 1 0.02646 1 1 30 2500 10
4 NULL rifle_ammo ammoRifle 5.56x45 HP 2 0.02646 1 1 30 5000 10
5 NULL pistol_ammo ammoPistol 9mm AP 3 0.01984 1 1 45 5000 12
6 NULL pistol_ammo ammoPistol 9mm FMJ 1 0.01984 1 1 45 2000 6
7 NULL pistol_ammo ammoPistol 9mm HP 2 0.01984 1 1 45 4000 6
8 NULL pistol_ammo ammoPistol .40 S&W AP 3 0.01984 1 1 45 5500 13
9 NULL pistol_ammo ammoPistol .40 S&W FMJ 1 0.01984 1 1 45 3000 7
10 NULL pistol_ammo ammoPistol .40 S&W HP 2 0.01984 1 1 45 4500 7
11 NULL rifle_ammo ammoRifle 7.62x39 AP 4 0.02646 1 1 30 5200 15
12 NULL rifle_ammo ammoRifle 7.62x39 FMJ 1 0.02646 1 1 30 1800 10
13 NULL rifle_ammo ammoRifle 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

View File

@ -1,52 +1,52 @@
id,type,name,rarity,weight,width,height,stack,value id,type,name,rarity,weight,width,height,stack,value
NULL,Basic Materials,Coal,2,0.02100,1,1,5,2500 NULL,matBasic,Coal,2,0.021,1,1,5,2500
NULL,Basic Materials,Wood,1,0.02100,1,1,20,1000 NULL,matBasic,Wood,1,0.021,1,1,20,1000
NULL,Basic Materials,Leather,2,0.25000,1,1,2,20000 NULL,matBasic,Leather,2,0.25,1,1,2,20000
NULL,Basic Materials,Cloth,1,0.03086,1,1,5,5000 NULL,matBasic,Cloth,1,0.03086,1,1,5,5000
NULL,Basic Materials,Plastic,1,0.22046,1,1,10,600 NULL,matBasic,Plastic,1,0.22046,1,1,10,600
NULL,Basic Materials,Thread,1,0.00772,1,1,20,250 NULL,matBasic,Thread,1,0.00772,1,1,20,250
NULL,Basic Materials,Acid,1,0.02100,1,1,10,2500 NULL,matBasic,Acid,1,0.021,1,1,10,2500
NULL,Basic Materials,Adhesive,2,0.03969,1,1,10,6000 NULL,matBasic,Adhesive,2,0.03969,1,1,10,6000
NULL,Basic Materials,Antiseptic,2,0.03969,1,1,2,5000 NULL,matBasic,Antiseptic,2,0.03969,1,1,2,5000
NULL,Basic Materials,Oil,2,0.06857,1,1,10,5000 NULL,matBasic,Oil,2,0.06857,1,1,10,5000
NULL,Basic Materials,Steel,1,0.06857,1,1,20,300 NULL,matBasic,Steel,1,0.06857,1,1,20,300
NULL,Basic Materials,Copper,2,0.06857,1,1,10,2000 NULL,matBasic,Copper,2,0.06857,1,1,10,2000
NULL,Basic Materials,Zinc,1,0.06857,1,1,10,400 NULL,matBasic,Zinc,1,0.06857,1,1,10,400
NULL,Basic Materials,Lead,2,0.34286,1,1,5,1500 NULL,matBasic,Lead,2,0.34286,1,1,5,1500
NULL,Basic Materials,Aluminum,2,0.06857,1,1,10,1200 NULL,matBasic,Aluminum,2,0.06857,1,1,10,1200
NULL,Basic Materials,Gold Scrap,5,0.06857,1,1,10,250000 NULL,matBasic,Gold Scrap,5,0.06857,1,1,10,250000
NULL,Basic Materials,Silver Scrap,4,0.06857,1,1,10,50000 NULL,matBasic,Silver Scrap,4,0.06857,1,1,10,50000
NULL,Basic Materials,Glass,1,0.03969,1,1,10,1000 NULL,matBasic,Glass,1,0.03969,1,1,10,1000
NULL,Basic Materials,Ceramic,2,0.22046,1,1,10,1000 NULL,matBasic,Ceramic,2,0.22046,1,1,10,1000
NULL,Tools,Hammer,1,1.25000,1,2,1,75000 NULL,tool,Hammer,1,1.25,1,2,1,75000
NULL,Tools,Flathead Screwdriver,1,0.33069,1,1,2,25000 NULL,tool,Flathead Screwdriver,1,0.33069,1,1,2,25000
NULL,Tools,Phillip Head Screwdriver,1,0.33069,1,1,2,25000 NULL,tool,Phillip Head Screwdriver,1,0.33069,1,1,2,25000
NULL,Tools,Set of Wrenches,3,3.00000,2,2,4,165000 NULL,tool,Set of Wrenches,3,3,2,2,4,165000
NULL,Tools,Large Pliers,1,0.33069,1,1,1,25000 NULL,tool,Large Pliers,1,0.33069,1,1,1,25000
NULL,Tools,Needle Nose Pliers,1,0.33069,1,1,1,25000 NULL,tool,Needle Nose Pliers,1,0.33069,1,1,1,25000
NULL,Tools,Measuring Tape,1,0.33069,1,1,1,40000 NULL,tool,Measuring Tape,1,0.33069,1,1,1,40000
NULL,Tools,Lock Pick,2,0.05000,1,1,5,50000 NULL,tool,Lock Pick,2,0.05,1,1,5,50000
NULL,Tools,Drill,3,4.00000,2,2,4,225000 NULL,tool,Drill,3,4,2,2,4,225000
NULL,Tools,Multi-Tool,4,0.25000,1,1,1,400000 NULL,tool,Multi-Tool,4,0.25,1,1,1,400000
NULL,Tools,Roll Pin Punch Set,3,3.00000,2,2,4,120000 NULL,tool,Roll Pin Punch Set,3,3,2,2,4,120000
NULL,Tools,Set of Files,3,2.00000,2,2,4,200000 NULL,tool,Set of Files,3,2,2,2,4,200000
NULL,Tools,Rotary Tool,3,1.50000,1,2,2,180000 NULL,tool,Rotary Tool,3,1.5,1,2,2,180000
NULL,Tools,Bench Vise,3,30.00000,2,2,4,350000 NULL,tool,Bench Vise,3,30,2,2,4,350000
NULL,Tools,Scissors,1,0.20000,2,1,2,35000 NULL,tool,Scissors,1,0.2,2,1,2,35000
NULL,Tools,Set of sockets,3,3.12500,2,1,2,50000 NULL,tool,Set of sockets,3,3.125,2,1,2,50000
NULL,Tools,Socket,1,0.31250,1,1,10,5000 NULL,tool,Socket,1,0.3125,1,1,10,5000
NULL,Tools,Wrench,1,0.60000,1,2,2,33000 NULL,tool,Wrench,1,0.6,1,2,2,33000
NULL,Tools,File,1,0.40000,1,2,2,40000 NULL,tool,File,1,0.4,1,2,2,40000
NULL,Tools,Ratchet,2,1.00000,1,2,2,25000 NULL,tool,Ratchet,2,1,1,2,2,25000
NULL,Tools,Needle,1,0.01544,1,1,10,200 NULL,tool,Needle,1,0.01544,1,1,10,200
NULL,Advanced Materials,Brass,3,0.06857,1,1,10,2500 NULL,matAdv,Brass,3,0.06857,1,1,10,2500
NULL,Advanced Materials,Tape,2,0.11023,1,1,1,5000 NULL,matAdv,Tape,2,0.11023,1,1,1,5000
NULL,Advanced Materials,Ballistic Fiber,3,0.57500,1,1,2,25000 NULL,matAdv,Ballistic Fiber,3,0.575,1,1,2,25000
NULL,Advanced Materials,Circuitry,3,0.22046,1,1,10,20000 NULL,matAdv,Circuitry,3,0.22046,1,1,10,20000
NULL,Advanced Materials,Springs,3,0.07938,1,1,5,10000 NULL,matAdv,Springs,3,0.07938,1,1,5,10000
NULL,Advanced Materials,Screws,3,0.07938,1,1,10,5000 NULL,matAdv,Screws,3,0.07938,1,1,10,5000
NULL,Advanced Materials,Gears,3,0.07938,1,1,5,8000 NULL,matAdv,Gears,3,0.07938,1,1,5,8000
NULL,Advanced Materials,Rubber,1,0.22046,1,1,10,5600 NULL,matAdv,Rubber,1,0.22046,1,1,10,5600
NULL,Junk,Car Battery,3,35.00000,3,2,6,500000 NULL,junk,Car Battery,3,35,3,2,6,500000
NULL,Junk,Pre-War Money,3,0.00050,1,1,100,5000 NULL,junk,Pre-War Money,3,0.0005,1,1,100,5000
NULL,Junk,Syringe,2,0.22046,1,1,1,7500 NULL,junk,Syringe,2,0.22046,1,1,1,7500

1 id type name rarity weight width height stack value
2 NULL Basic Materials matBasic Coal 2 0.02100 0.021 1 1 5 2500
3 NULL Basic Materials matBasic Wood 1 0.02100 0.021 1 1 20 1000
4 NULL Basic Materials matBasic Leather 2 0.25000 0.25 1 1 2 20000
5 NULL Basic Materials matBasic Cloth 1 0.03086 1 1 5 5000
6 NULL Basic Materials matBasic Plastic 1 0.22046 1 1 10 600
7 NULL Basic Materials matBasic Thread 1 0.00772 1 1 20 250
8 NULL Basic Materials matBasic Acid 1 0.02100 0.021 1 1 10 2500
9 NULL Basic Materials matBasic Adhesive 2 0.03969 1 1 10 6000
10 NULL Basic Materials matBasic Antiseptic 2 0.03969 1 1 2 5000
11 NULL Basic Materials matBasic Oil 2 0.06857 1 1 10 5000
12 NULL Basic Materials matBasic Steel 1 0.06857 1 1 20 300
13 NULL Basic Materials matBasic Copper 2 0.06857 1 1 10 2000
14 NULL Basic Materials matBasic Zinc 1 0.06857 1 1 10 400
15 NULL Basic Materials matBasic Lead 2 0.34286 1 1 5 1500
16 NULL Basic Materials matBasic Aluminum 2 0.06857 1 1 10 1200
17 NULL Basic Materials matBasic Gold Scrap 5 0.06857 1 1 10 250000
18 NULL Basic Materials matBasic Silver Scrap 4 0.06857 1 1 10 50000
19 NULL Basic Materials matBasic Glass 1 0.03969 1 1 10 1000
20 NULL Basic Materials matBasic Ceramic 2 0.22046 1 1 10 1000
21 NULL Tools tool Hammer 1 1.25000 1.25 1 2 1 75000
22 NULL Tools tool Flathead Screwdriver 1 0.33069 1 1 2 25000
23 NULL Tools tool Phillip Head Screwdriver 1 0.33069 1 1 2 25000
24 NULL Tools tool Set of Wrenches 3 3.00000 3 2 2 4 165000
25 NULL Tools tool Large Pliers 1 0.33069 1 1 1 25000
26 NULL Tools tool Needle Nose Pliers 1 0.33069 1 1 1 25000
27 NULL Tools tool Measuring Tape 1 0.33069 1 1 1 40000
28 NULL Tools tool Lock Pick 2 0.05000 0.05 1 1 5 50000
29 NULL Tools tool Drill 3 4.00000 4 2 2 4 225000
30 NULL Tools tool Multi-Tool 4 0.25000 0.25 1 1 1 400000
31 NULL Tools tool Roll Pin Punch Set 3 3.00000 3 2 2 4 120000
32 NULL Tools tool Set of Files 3 2.00000 2 2 2 4 200000
33 NULL Tools tool Rotary Tool 3 1.50000 1.5 1 2 2 180000
34 NULL Tools tool Bench Vise 3 30.00000 30 2 2 4 350000
35 NULL Tools tool Scissors 1 0.20000 0.2 2 1 2 35000
36 NULL Tools tool Set of sockets 3 3.12500 3.125 2 1 2 50000
37 NULL Tools tool Socket 1 0.31250 0.3125 1 1 10 5000
38 NULL Tools tool Wrench 1 0.60000 0.6 1 2 2 33000
39 NULL Tools tool File 1 0.40000 0.4 1 2 2 40000
40 NULL Tools tool Ratchet 2 1.00000 1 1 2 2 25000
41 NULL Tools tool Needle 1 0.01544 1 1 10 200
42 NULL Advanced Materials matAdv Brass 3 0.06857 1 1 10 2500
43 NULL Advanced Materials matAdv Tape 2 0.11023 1 1 1 5000
44 NULL Advanced Materials matAdv Ballistic Fiber 3 0.57500 0.575 1 1 2 25000
45 NULL Advanced Materials matAdv Circuitry 3 0.22046 1 1 10 20000
46 NULL Advanced Materials matAdv Springs 3 0.07938 1 1 5 10000
47 NULL Advanced Materials matAdv Screws 3 0.07938 1 1 10 5000
48 NULL Advanced Materials matAdv Gears 3 0.07938 1 1 5 8000
49 NULL Advanced Materials matAdv Rubber 1 0.22046 1 1 10 5600
50 NULL Junk junk Car Battery 3 35.00000 35 3 2 6 500000
51 NULL Junk junk Pre-War Money 3 0.00050 0.0005 1 1 100 5000
52 NULL Junk junk Syringe 2 0.22046 1 1 1 7500

7
Database/id.py Normal file
View File

@ -0,0 +1,7 @@
# starting_ids.py
starting_ids = {
"reloading": 20000,
"ammo": 10000,
"craft": 30000
}

View File

@ -1,6 +1,7 @@
import csv import csv
import os import os
import sqlite3 import sqlite3
from id import starting_ids
# Connect to the SQLite database # Connect to the SQLite database
conn = sqlite3.connect("items.db") conn = sqlite3.connect("items.db")
@ -9,41 +10,12 @@ cursor = conn.cursor()
# Define the directory where the CSV files are located # Define the directory where the CSV files are located
csv_directory = "data" # Change this to your directory path csv_directory = "data" # Change this to your directory path
# Define the CSV file names # Define the starting IDs for each table type
csv_file_reloading = "reloading.csv" # starting_ids = {"reloading": 20000, "ammo": 10000, "craft": 30000}
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})"
)
# Function to load data from a CSV file into a table # 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 existing data in the table
delete_query = f"DELETE FROM {table_name}" delete_query = f"DELETE FROM {table_name}"
cursor.execute(delete_query) 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 # Exclude the first column (id) from the row
values = row[1:] values = row[1:]
placeholders = ", ".join(["?"] * len(values)) placeholders = ", ".join(["?"] * len(values))
insert_query = f"INSERT INTO {table_name} VALUES (NULL, {placeholders})" insert_query = f"INSERT INTO {table_name} VALUES (?, {placeholders})"
cursor.execute(insert_query, values) cursor.execute(insert_query, [starting_id] + values)
starting_id += 1
# Load data from the reloading CSV file into the reloading table # Get a list of CSV files in the data directory
load_csv_data(csv_path_reloading, "reloading", cursor) 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 # Loop through the CSV files and load data into respective tables
load_csv_data(csv_path_ammo, "ammo", cursor) for csv_file in csv_files:
table_name = os.path.splitext(csv_file)[
# Load data from the ammo CSV file into the ammo table 0
load_csv_data(csv_path_craft, "craft", cursor) ] # 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 # Commit the changes and close the connection
conn.commit() conn.commit()

Binary file not shown.