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)
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})
db.commit()
if __name__ == "__main__":
main()
The error message:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1246, in _execute_context
cursor, statement, parameters, context
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 581, in do_execute
cursor.execute(statement, parameters)
psycopg2.DataError: invalid input syntax for integer: "080213825X"
LINE 1: ...RT INTO books (isbn, title, author, year) VALUES ('080213825...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "import.py", line 22, in <module>
main()
File "import.py", line 17, in main
{"isbn": isbn, "title": title, "author": author, "year": year}
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/scoping.py", line 162, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1269, in execute
clause, params or {}
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 982, in execute
return meth(self, multiparams, params)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 287, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1101, in _execute_clauseelement
distilled_params,
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1250, in _execute_context
e, statement, parameters, cursor, context
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1476, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1246, in _execute_context
cursor, statement, parameters, context
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 581, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.DataError: (psycopg2.DataError) invalid input syntax for integer: "080213825X"
LINE 1: ...RT INTO books (isbn, title, author, year) VALUES ('080213825...
^
[SQL: INSERT INTO books (isbn, title, author, year) VALUES (%(isbn)s, %(title)s, %(author)s, %(year)s)]
[parameters: {'isbn': '080213825X', 'title': 'Four Blondes', 'author': 'Candace Bushnell', 'year': '2000'}]
(Background on this error at: http://sqlalche.me/e/9h9h)
I've found a similar question here https://stackoverflow.com/questions/53359079/cs50-web-programming-postgres-sql-error-while-importing-books-csv-file The solution was "I remade the tables using adminer.cs50.net and I was able to fill the columns with csv data" I tried doing this but it didn't help me.