added main, creates tables then imports csv

This commit is contained in:
0ceanSlim 2023-09-04 15:45:15 -04:00
parent 37620b4892
commit 9053ece28e
5 changed files with 128 additions and 38 deletions

57
Database/README.md Normal file
View File

@ -0,0 +1,57 @@
# Database Tooling for Managing SQLite Databases
This set of Python scripts and tools allows you to create and manage SQLite databases for storing and manipulating data from CSV files. It is designed to help you easily import data from CSV files into SQLite tables and set up the database schema.
## Prerequisites
Before using these tools, ensure that you have Python installed on your system. You'll also need to have the required CSV files that you want to import into the database.
## Getting Started
1. **Clone the Repository**: Start by cloning this repository to your local machine or download the provided scripts.
2. **Modify Script Variables**:
- Open the `main.py` script and update the paths and variables according to your specific use case:
- `create_tables_script`: Set the path to your `createTables.py` script.
- `import_data_script`: Set the path to your `importData.py` script.
- Open the `createTables.py` script and define the database schema for your tables.
- Update the CSV file names and paths in the `importData.py` script to match your data files.
3. **Run the Main Script**:
- Execute the `main.py` script using the following command:
```bash
python main.py
```
- The script will first create the tables based on the schema defined in `createTables.py`.
- It will then import data from your CSV files into the respective tables.
- If any errors occur during these processes, they will be displayed in the console.
4. **Database Management**:
- You can further customize the database schema and scripts to meet your specific needs.
- The provided scripts handle resetting ID sequences and deleting existing data, so be cautious when using them on production databases.
## Database Structure
The database created by these scripts contains two tables: `reloading` and `ammo`. The schema for these tables can be customized in the `createTables.py` script.
## Customization
You can customize the following aspects of this tooling:
- Database schema in `createTables.py`.
- CSV file names and paths in `importData.py`.
- Starting ID values for each table.
- CSV data loading logic in `load_csv_data` function.
## Troubleshooting
If you encounter any issues or errors while using this tooling, please refer to the error messages displayed in the console for guidance.
## License
This project is licensed under the MIT License. Feel free to modify and distribute it as needed for your projects.
---
Happy database management!

43
Database/createTables.py Normal file
View File

@ -0,0 +1,43 @@
import sqlite3
# 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
)
"""
)
# 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
)
"""
)
# Close the connection
conn.close()

View File

@ -21,46 +21,15 @@ csv_path_ammo = os.path.join(csv_directory, csv_file_ammo)
starting_id_reloading = 20000 # Replace with your desired starting value
starting_id_ammo = 10000 # Replace with your desired starting value
# 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
)
"""
)
# Define the table schema for new_table
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
)
"""
)
# 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})")
# Reset the ammo table's id sequence
cursor.execute(f"DELETE FROM SQLITE_SEQUENCE WHERE name='ammo'")
# 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})"
)
@ -86,7 +55,7 @@ def load_csv_data(csv_path, table_name, cursor):
# Load data from the reloading CSV file into the reloading table
load_csv_data(csv_path_reloading, "reloading", cursor)
# Load data from the new_table CSV file into the new_table table
# Load data from the ammo CSV file into the ammo table
load_csv_data(csv_path_ammo, "ammo", cursor)
# Commit the changes and close the connection

Binary file not shown.

21
Database/main.py Normal file
View File

@ -0,0 +1,21 @@
import subprocess
# Define the paths to your Python scripts
create_tables_script = "createTables.py"
import_data_script = "importData.py"
# Run the createTables.py script
try:
subprocess.run(["python", create_tables_script], check=True)
print("createTables.py script executed successfully.")
except subprocess.CalledProcessError:
print("Error running createTables.py script.")
exit(1)
# Run the importData.py script
try:
subprocess.run(["python", import_data_script], check=True)
print("importData.py script executed successfully.")
except subprocess.CalledProcessError:
print("Error running importData.py script.")
exit(1)