File size: 1,010 Bytes
3e30d53 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # create_dummy_db.py
import sqlite3
# Connect to the SQLite database (this will create the file if it doesn't exist)
conn = sqlite3.connect('portfolio.db')
cursor = conn.cursor()
# --- Create the holdings table ---
cursor.execute('''
CREATE TABLE IF NOT EXISTS holdings (
id INTEGER PRIMARY KEY,
symbol TEXT NOT NULL UNIQUE,
shares INTEGER NOT NULL,
average_cost REAL NOT NULL
)
''')
print("Table 'holdings' created successfully.")
# --- Insert some sample data ---
# Using INSERT OR IGNORE to prevent errors if you run the script multiple times
holdings_data = [
('NVDA', 1500, 250.75),
('AAPL', 5000, 180.20),
('IBM', 2500, 155.45),
('TSLA', 1000, 220.90)
]
cursor.executemany('''
INSERT OR IGNORE INTO holdings (symbol, shares, average_cost) VALUES (?, ?, ?)
''', holdings_data)
print(f"{len(holdings_data)} sample holdings inserted.")
# --- Commit the changes and close the connection ---
conn.commit()
conn.close()
print("Database 'portfolio.db' is set up and ready.") |