-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
90 lines (77 loc) · 2.58 KB
/
db.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import tomllib
import psycopg
import asyncio
import pandas as pd
def get_config():
path = "config.toml"
with open(path, "rb") as f:
config = tomllib.load(f)
return config
def get_db_conn_string():
config = get_config()
return(f"""
dbname={config["db"]["db"]}
user={config["db"]["user"]}
password={config["db"]["password"]}
host={config["db"]["host"]}
port={config["db"]["port"]}
""")
async def create_table():
conn_str = get_db_conn_string()
async with await psycopg.AsyncConnection.connect(conn_str) as aconn:
async with aconn.cursor() as acur:
await acur.execute("""
CREATE TABLE IF NOT EXISTS exercises_manual (
date date not null,
activity text not null,
distance integer not null,
duration integer not null,
location text not null,
elsa integer not null,
ascent float not null,
pull integer not null,
weight float not null
);
""")
async def insert():
conn_str = get_db_conn_string()
async with await psycopg.AsyncConnection.connect(conn_str) as aconn:
async with aconn.cursor() as acur:
data = pd.read_csv("ski.csv")
for index, row in data.iterrows():
query = """
INSERT INTO exercises_manual (
date,
activity,
distance,
duration,
location,
elsa,
ascent,
pull,
weight
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
try:
await acur.execute(
query,
(
row.Date,
row.Activity,
row.Distance,
row.Time,
row.Location,
row.Elsa,
row.Elevation,
row.pull,
row.weight
)
)
except Exception as e:
print(f"Error inserting: {e}")
print("Done inserting")
async def main():
await create_table()
await insert()
if __name__ == "__main__":
asyncio.run(main())