-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.py
59 lines (43 loc) · 1.46 KB
/
import.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
import os
import csv
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
DATABASE_URL="postgresql://localhost/book_review"
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
def main():
f = open("books.csv")
reader = csv.reader(f)
try:
for isbn, tit, au, yr in reader:
print (isbn, tit, au, yr)
db.execute('INSERT INTO books_des(isbn_num, title, author, publish_year) VALUES(:isbn, :title, :author, :year)',
{'isbn':isbn, 'title':tit, 'author':au, 'year':yr})
print("{tit} successfully added")
except Exception as e:
print ("THE ERROR IS", e)
db.commit()
if __name__ == "__main__":
main()
# def import_to_db(file):
# f = open(file)
# reader = csv.reader(f)
# row_count = sum(1 for row in reader) - 1
# i = 0
# #print(reader)
# for row in reader:
# # for isbn, title, author, year in reader:
# # print(isbn)
# # db.execute("INSERT INTO books_des (isbn_num, title, author, publish_year) VALUES(:isbn_num, :title, :author, :publish_year)",\
# # {'isbn_num': isbn, 'title': title, 'author': author, 'publish_year': year})
# # i += 1
# # db.commit()
# if i == row_count:
# print("You successfully imported the data to your database.")
# else:
# print(row_count)
# print("Looks like something is wrong!")
# if __name__ == "__main__":
# filename = input("Enter the name of the csv file: ")
# import_to_db(filename)
# #db.execute("SELECT * FROM books_des")