projectEli/Database/reloading.py

51 lines
1.3 KiB
Python

import sqlite3
import csv
import os
# Connect to the SQLite database
conn = sqlite3.connect("items.db")
cursor = conn.cursor()
# Drop the existing reloading table if it exists
cursor.execute("DROP TABLE IF EXISTS reloading")
# Create the reloading table with an auto-incremented primary key
cursor.execute(
"""
CREATE TABLE reloading (
id INTEGER PRIMARY KEY AUTOINCREMENT,
type TEXT,
name TEXT,
rarity INTEGER,
weight REAL,
width INTEGER,
height INTEGER,
stack INTEGER,
value INTEGER
)
"""
)
# Define the directory where the CSV file is located
csv_directory = "data" # Change this to your directory path
# Define the CSV file name
csv_file = "reloading.csv" # Change this to your CSV file name
# Build the full path to the CSV file
csv_path = os.path.join(csv_directory, csv_file)
# Read data from the CSV file and insert it into the table
with open(csv_path, newline="") as csvfile:
csv_reader = csv.reader(csvfile)
next(csv_reader) # Skip the header row if it exists in the CSV
for row in csv_reader:
cursor.execute(
"INSERT INTO reloading (type, name, rarity, weight, width, height, stack, value) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
row,
)
# Commit the changes and close the connection
conn.commit()
conn.close()