Re: ERROR when inserting csv values into sql table - Mailing list pgsql-novice
From | Mark Wallace |
---|---|
Subject | Re: ERROR when inserting csv values into sql table |
Date | |
Msg-id | 44284D32-569C-4134-85FD-D06607FBBB78@dataxdesign.com Whole thread Raw |
In response to | RE: ERROR when inserting csv values into sql table (Cravan <savageapple850@gmail.com>) |
Responses |
Re: ERROR when inserting csv values into sql table
|
List | pgsql-novice |
As Anthony E. Greene already mentioned yesterday (06/12/19 @ 08:07am EDT) in a comment on this thread, you are data typing imdbRating as INTEGER, when it should be NUMERIC.
Try again with this DDL:
CREATE TABLE movies ("title" SERIAL PRIMARY KEY,"year" INTEGER NOT NULL,"runtime" INTEGER NOT NULL,"imdbID" VARCHAR NOT NULL,"imdbRating” NUMERIC NOT NULL);
Mark
On Jun 13, 2019, at 03:40, Cravan <savageapple850@gmail.com> wrote:Hi guys,Have edited the code, but Im still getting an error. I have already edited my apostrophes and spacing and looked up examples from google to try and resolve my error to no avail. I also ensured that i defined DATABASE_URL properly. Sorry for the long traceback error at the end :P Please note that my csv values are stored in lists in each cell. They are arranged in a single column such asThe Lego Movie;2014;100;tt1490017;7.8 <— The last value (7.8) is of type numeric, not integer.This is my main code
import csvimport sysimport osfrom sqlalchemy import Column, ForeignKey, Integer, Stringfrom sqlalchemy import create_enginefrom flask import Flask, render_template, request, sessionfrom flask_sqlalchemy import SQLAlchemyfrom flask_session import Sessionengine = create_engine(os.getenv("DATABASE_URL")) # database engine object from SQLAlchemy that manages connections to the database,# DATABASE_URL is an environment variable that indicates where the database livesdef main():f = open("movies.csv","r")reader = csv.reader(f)for row in f: # loop gives each column a namevals = row.split(';')title = vals[0]year = vals[1]runtime = vals[2]imdbID = vals[3]imdbRating = vals[4]engine.execute('INSERT INTO movies("title","year","runtime","imdbID","imdbRating") VALUES ((title), (year), (runtime), (imdbID), (imdbRating))',{"title": title, "year": year, "runtime": runtime, "imdbID": imdbID, "imdbRating": imdbRating })engine.commit() # transactions are assumed, so close the transaction finishedif __name__ == "__main__":main()SQL code:
CREATE TABLE movies ("title" SERIAL PRIMARY KEY,"year" INTEGER NOT NULL,"runtime" INTEGER NOT NULL,"imdbID" VARCHAR NOT NULL,"imdbRating" INTEGER NOT NULL);New error code:
#############################
Traceback (most recent call last):File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1244, in _execute_contextcursor, statement, parameters, contextFile "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 550, in do_executecursor.execute(statement, parameters)psycopg2.errors.UndefinedColumn: column "title" does not existLINE 1: ...,"year","runtime","imdbID","imdbRating") VALUES ((title), (y...^HINT: There is a column named "title" in table "movies", but it cannot be referenced from this part of the query.The above exception was the direct cause of the following exception:Traceback (most recent call last):File "import.py", line 26, in <module>main()File "import.py", line 23, in main{"title": title, "year": year, "runtime": runtime, "imdbID": imdbID, "imdbRating": imdbRating })File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2166, in executereturn connection.execute(statement, *multiparams, **params)File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 982, in executereturn self._execute_text(object_, multiparams, params)File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1155, in _execute_textparameters,File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1248, in _execute_contexte, statement, parameters, cursor, contextFile "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1466, in _handle_dbapi_exceptionutil.raise_from_cause(sqlalchemy_exception, exc_info)File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 383, in raise_from_causereraise(type(exception), exception, tb=exc_tb, cause=cause)File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 128, in reraiseraise value.with_traceback(tb)File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1244, in _execute_contextcursor, statement, parameters, contextFile "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 550, in do_executecursor.execute(statement, parameters)sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedColumn) column "title" does not existLINE 1: ...,"year","runtime","imdbID","imdbRating") VALUES ((title), (y...^HINT: There is a column named "title" in table "movies", but it cannot be referenced from this part of the query.[SQL: INSERT INTO movies("title","year","runtime","imdbID","imdbRating") VALUES ((title), (year), (runtime), (imdbID), (imdbRating))][parameters: {'title': 'Title', 'year': 'Year', 'runtime': 'Runtime', 'imdbID': 'imdbID', 'imdbRating': 'imdbRating\n'}]Here’s a pic of the new table:<image001.png>Thanks,Cravan
pgsql-novice by date: