0

I am currently trying to import the contents of the books.csv file into my PostgreSQL database on Adminer.

Here's my code:

import csv
import os

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))

def main():
    f = open("books.csv")
    reader = csv.reader(f)
    next(reader, None)
    for isbn, title, author, year in reader:
        db.execute("INSERT INTO books (isbn, title, author, year) VALUES (:isbn, :title, :author, :year)",
            {"isbn": isbn, "title": title, "author": author, "year": year})
        print(f"Added book with isbn {isbn}.")
db.commit()

if __name__ == "__main__":
    main()

I have set the DATABASE_URI to what was given to me as the URI field in my Heroku Postgres :: Database credentials.

But the problem I am facing is that the code compiles and even runs properly, but after more than 20 minutes of runtime, I am not able to see any entries in my database table online on Adminer.

Any help on where I could have gone wrong would be kindly appreciated.

3
  • Hi, I'm having the same problem. Were you able to solve it? If so, could you please help me? – cristobal chavez May 23 '20 at 1:51
  • @cristobalchavez, there was no problem, the program's running fine, but I think that since the database is huge, it takes a lot of time to actually put the entries into the database table. I refreshed my database on Adminer after some time and the entries were there... – Shrey Tripathi May 23 '20 at 7:57
  • Thank you! I'll just wait then – cristobal chavez May 23 '20 at 19:43
0

I wrote the same code but I didn't use the URI on import.py . I just excecuted cmd before running import.py, setting my database.

set DATABASE_URL = "postgres://....."

and it's working properly

I don't know if it helps but just in case!

2
  • Thank you for your reply. I did exactly the same thing as you, but am still facing the issue. One more thing, what was the runtime of your program? I am just curious, since mine took more than 20 minutes.... – Shrey Tripathi May 18 '20 at 5:45
  • It took a while, but less than 10 minutes for sure and it filled my database on Adminer. I only executed the code once! – Juliet May 18 '20 at 17:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .