forked from meta-llama/llama-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2db.py
38 lines (30 loc) · 1.1 KB
/
csv2db.py
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
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}')