0

I'm on a project1, here is the link to it: https://docs.cs50.net/ocw/web/projects/1/project1.html

I've successfully made a Table books using my Python script, however when I try to import the "books.csv" file into the table a Postgres SQL error occurs.

Here is 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)
    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 from {title}")
    db.commit()



if __name__ == "__main__":
    main()

And the error my import.py file showing is:

python: can't open file 'index.py': [Errno 2] No such file or directory

D:\coding\project1>python import.py Traceback (most recent call last): File "C:\Users\ayush\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\engine\base.py", line 1283, in _execute_context self.dialect.do_execute( File "C:\Users\ayush\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\engine\default.py", line 590, in do_execute cursor.execute(statement, parameters) psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type integer: "year" LINE 1: ...tle, author,year) VALUES ('isbn', 'title', 'author', 'year')

**Edit: I added a Postgres extension to my IDE, it works fine now. **

2
  • There is a missing ) before this line. Commented May 30, 2020 at 10:34
  • Traceback (most recent call last): File "C:\Users\ayush\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\engine\base.py", line 1283, in _execute_context self.dialect.do_execute( File "C:\Users\ayush\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\engine\default.py", line 590, in do_execute cursor.execute(statement, parameters) psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type integer: "year" LINE 1: ...tle, author,year) VALUES ('isbn', 'title', 'author', 'year') Commented May 30, 2020 at 10:49

1 Answer 1

0

You have a problem with print, you forget to close it.

print(f"Added book from {title}")

You must log in to answer this question.

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