csv2db.py 1.26 KB
Newer Older
Rayyyyy's avatar
Rayyyyy committed
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
37
38
39
40
41
# Copyright (c) Meta Platforms, Inc. and affiliates.
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.

import sqlite3
import csv

# Define the input CSV file and the SQLite database file
input_csv = 'nba_roster.csv'
database_file = 'nba_roster.db'

# Connect to the SQLite database
conn = sqlite3.connect(database_file)
cursor = conn.cursor()

# Create a table to store the data
cursor.execute('''CREATE TABLE IF NOT EXISTS nba_roster (
                    Team TEXT,
                    NAME TEXT,
                    Jersey TEXT,
                    POS TEXT,
                    AGE INT,
                    HT TEXT,
                    WT TEXT,
                    COLLEGE TEXT,
                    SALARY TEXT
                )''')

# Read data from the CSV file and insert it into the SQLite table
with open(input_csv, 'r', newline='') as csvfile:
    csv_reader = csv.reader(csvfile)
    next(csv_reader)  # Skip the header row
    
    for row in csv_reader:
        cursor.execute('INSERT INTO nba_roster VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', row)

# Commit the changes and close the database connection
conn.commit()
conn.close()

print(f'Data from {input_csv} has been successfully imported into {database_file}')