From 9053ece28ecb58963b2af73ef8aed61a47c31466 Mon Sep 17 00:00:00 2001 From: 0ceanSlim <89587889+0ceanSlim@users.noreply.github.com> Date: Mon, 4 Sep 2023 15:45:15 -0400 Subject: [PATCH] added main, creates tables then imports csv --- Database/README.md | 57 ++++++++++++++++++++++++++ Database/createTables.py | 43 +++++++++++++++++++ Database/{update.py => importData.py} | 45 ++++---------------- Database/items.db | Bin 77824 -> 77824 bytes Database/main.py | 21 ++++++++++ 5 files changed, 128 insertions(+), 38 deletions(-) create mode 100644 Database/README.md create mode 100644 Database/createTables.py rename Database/{update.py => importData.py} (70%) create mode 100644 Database/main.py diff --git a/Database/README.md b/Database/README.md new file mode 100644 index 00000000..58efbcab --- /dev/null +++ b/Database/README.md @@ -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! diff --git a/Database/createTables.py b/Database/createTables.py new file mode 100644 index 00000000..a638ab7c --- /dev/null +++ b/Database/createTables.py @@ -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() diff --git a/Database/update.py b/Database/importData.py similarity index 70% rename from Database/update.py rename to Database/importData.py index 440b4a85..ceed14fc 100644 --- a/Database/update.py +++ b/Database/importData.py @@ -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 diff --git a/Database/items.db b/Database/items.db index c66e664c003dd9c52939398f07cd198806accd29..17bd6a09e6746c6f4805df02508a771fa8ebfce6 100644 GIT binary patch delta 217 zcmZp8z|!!5Wr8$g=tLQ3#?Xxkhxr*7ZB`K|;1%RPC&0}B2fF8EC+AA)ZN4mfh@Vk! z@_Jh>=Ega?lW*DT0coAdoOU2ud$I+X)|#Adrv~I}PIlB1nfzZ<49M1)yhcxlQ5`4( z(y2C?-(DR^t4=o1*JV_hd_YSZ$WsRLL?^G;0I64+yxU%xMZR&4;^v3?3Sg%&@jqqY s|G@urGmpbferZl7W>KcZ+}wP1Sw2Q)d8VS&oczR;%)E3zy+r{J0N^G;cmMzZ delta 217 zcmZp8z|!!5Wr8%L??f4AM&FGIhxr+&ZdMT};1y)hZJguI&7i2v$j-^hquea4=Zk*#e`IfC7koK6&X$PX+CtHANx5?>tYCyj0WJfKL$^SLQfNYn^ zYxHy&oq-}ColcYa?bU&_<75MUT}Fq=2eh<-JbNHdbn