csv2db.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright (c) Meta Platforms, Inc. and affiliates.
  2. # This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.
  3. import sqlite3
  4. import csv
  5. # Define the input CSV file and the SQLite database file
  6. input_csv = 'nba_roster.csv'
  7. database_file = 'nba_roster.db'
  8. # Connect to the SQLite database
  9. conn = sqlite3.connect(database_file)
  10. cursor = conn.cursor()
  11. # Create a table to store the data
  12. cursor.execute('''CREATE TABLE IF NOT EXISTS nba_roster (
  13. Team TEXT,
  14. NAME TEXT,
  15. Jersey TEXT,
  16. POS TEXT,
  17. AGE INT,
  18. HT TEXT,
  19. WT TEXT,
  20. COLLEGE TEXT,
  21. SALARY TEXT
  22. )''')
  23. # Read data from the CSV file and insert it into the SQLite table
  24. with open(input_csv, 'r', newline='') as csvfile:
  25. csv_reader = csv.reader(csvfile)
  26. next(csv_reader) # Skip the header row
  27. for row in csv_reader:
  28. cursor.execute('INSERT INTO nba_roster VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', row)
  29. # Commit the changes and close the database connection
  30. conn.commit()
  31. conn.close()
  32. print(f'Data from {input_csv} has been successfully imported into {database_file}')