-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.py
73 lines (59 loc) · 2.24 KB
/
sql.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# sql only
import pandas as pd
import csv
import sqlite3
player_career_info_df = pd.read_excel('Player Career Info.xlsx')
player_career_info_df.to_csv('player_career_info.csv', index=False)
'''
# EXECUTED ON 1-12-2025
try:
# import csv
with open('player_career_info.csv', 'r', encoding='utf-8') as file:
dict_reader = csv.DictReader(file)
player_data = [(x['player_id'],
x['player'],
x['birth_year'],
x['hof'],
x['num_seasons'],
x['first_season'],
x['last_season'])
for x in dict_reader]
print(player_data)
# connect to db
conn = sqlite3.connect('player_career_info_2025_01_12.db')
cur = conn.cursor()
# set up player_table in variable
player_table = 'CREATE TABLE player(player_id INT NOT NULL,\
player VARCHAR(50) NOT NULL, \
birth_year INT, \
hof VARCHAR(10) NOT NULL, \
num_seasons INT NOT NULL, \
first_season VARCHAR(10) NOT NULL, \
last_season VARCHAR(10) NOT NULL);'
# create player_table
cur.execute(player_table)
# insert data into player table
cur.executemany('INSERT INTO player (player_id, player, birth_year, hof, num_seasons, first_season, last_season) \
VALUES (?,?,?,?,?,?,?);', player_data)
# display player table
cur.execute('SELECT * FROM player')
result = cur.fetchall()
print(result)
conn.commit() # commit changes
if conn:
print('data ingestion success')
cur.close() # close db
except sqlite3.Error as error:
print(f'error: ', error)
'''
# EXECUTED ON 1-12-2025 - QUERY TEST = SUCCESS
try:
# connect to db
conn = sqlite3.connect('player_career_info_2025_01_12.db')
cur = conn.cursor()
# display player table
cur.execute("SELECT * FROM player WHERE num_seasons >= 21 AND last_season <= 2025")
result = cur.fetchall()
print(result)
except sqlite3.Error as error:
print(f'error: ', error)