-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwrite_csv_to_db.py
31 lines (26 loc) · 1016 Bytes
/
write_csv_to_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
"""
See https://www.sqlalchemy.org/ - SQLAlchemy must be installed in your virtualenv.
I used a PostgreSQL db on Heroku.
Your empty database table must already exist.
Table fieldnames must be set.
The environment variable "DATABASE_URL" must be set on the computer this runs on.
"""
import csv
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine(os.getenv("DATABASE_URL")) # environment variable
db = scoped_session(sessionmaker(bind=engine))
def main():
f = open("books.csv")
reader = csv.reader(f)
# name of each field (column) in this csv - don't leave any out
for isbn, title, author, year in reader:
# SQL insert into table named books
db.execute( "INSERT INTO books (isbn, title, author, year) VALUES (:isbn, :title, :author, :year)",
{"isbn": isbn, "title": title, "author": author, "year": year} )
db.commit()
# close the file
f.close()
if __name__ == "__main__":
main()