How to solve invalid input syntax for type double precision

JJL

I try to load csv data into postgres. The creating table part is fine. But when I try to load data from csv, it got error. My code and error are attached below. Is %s wrong?

import psycopg2
import csv 

conn = psycopg2.connect(host="127.0.0.1", port="5432", database="postgres", user="postgres", password="*******")
print "Opened database successfully"

cur = conn.cursor()     
cur.execute('''create table calls_aapl("Ask" float,"Bid" float,"Change" float,"ContractSymbol" varchar(50),"ImpliedVolatility" float,"LastPrice" float,
                  "LastTradeDate" date,"OpenInterest" int,"PercentChange" float,"Strike" float,"Volume" int);''')
print "Table created successfully"

reader = csv.reader(open('D:/python/Anaconda/AAPL_Data/Calls.csv', 'r'))

for i, row in enumerate(reader):
    print(i, row)
    if i == 0: continue


    cur.execute('''
        INSERT INTO "calls_aapl"(
            "Ask", "Bid", "Change", "ContractSymbol", "ImpliedVolatility", "LastPrice", "LastTradeDate", "OpenInterest", "PercentChange", "Strike", "Volume"
        ) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)''', row
    )

conn.commit()
cur.close()

Error:

(0, ['Ask', 'Bid', 'Change', 'ContractSymbol', 'LastPrice', 'LastTradeDate', 'OpenInterest', 'PercentChange', 'PercentImpliedVolatility', 'Strike', 'Volume'])
(1, ['41.7', '39.75', '1.15', 'AAPL180803C00150000', '41.05', '7/31/2018', '52', '2.88', '154.59', '150', '6'])
DataError: invalid input syntax for type double precision: "7/31/2018"
LINE 4: ...1.7','39.75','1.15','AAPL180803C00150000','41.05','7/31/2018...
                                                             ^
Laurenz Albe

Using %s is ok because PostgreSQL can cast strings to numbers in an INSERT.

Your problem is a different one. Your INSERT statement specifies a column "ImpliedVolatility" (too late for a warning against mixed case identifiers) which is not in the data.

This causes the fifth column (labeled LastPrice to be inserted into "ImpliedVolatility" and the next column (labeled LastTradeDate) to be inserted into "LastPrice".

The former of these is wrong but works, because both "LastPrice" and "ImpliedVolatility" are float^H^H^H^H^Hdouble precision, but the latter fails because it tries to insert a date string into a double precision column.

Omit the column "ImpliedVolatility" from the INSERT statement.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Invalid input syntax for type double precision

From Dev

rails postgres ERROR: invalid input syntax for type double precision

From Dev

rails postgres ERROR: invalid input syntax for type double precision

From Dev

Invalid input syntax for type interval

From Dev

Invalid Input Syntax for Type Date

From Dev

How to increase precision of double type for large numbers?

From Dev

PostgreSQL - Error: "invalid input syntax for type bytea"

From Dev

"invalid input syntax for type numeric" for entering "emptyness"

From Dev

Postgres COPY FROM Fails With invalid input syntax for type timestamp

From Dev

Invalid input syntax for type date: "\N" in postgresql when fetching data

From Dev

pg nodejs package results in 'invalid input syntax for type json'

From Dev

ActiveRecord::StatementInvalid (PG::InvalidDatetimeFormat: ERROR: invalid input syntax for type date: ""

From Dev

Astyanax Cassandra Double type precision

From Dev

Astyanax Cassandra Double type precision

From Dev

double/float type precision in Python

From Dev

How to solve error: "Invalid type in JSON write (NSConcreteData)"

From Dev

How to solve Type mismatch issue (expected: Double, actual: Unit)

From Dev

How to store double with maximum precision

From Dev

Error: invalid input syntax for integer: ""

From Dev

Error: invalid input syntax for integer: ""

From Dev

Invalid input syntax PLPGSQL script

From Dev

invalid input syntax for integer (string)

From Dev

Create column of type double precision[] with liquibase

From Dev

Hive Data Type : Double Precision & Scale

From Dev

How to solve the Total of my html code that linking with input type = "number"

From Dev

How to solve 'Undefined Index' Error from <input type="radio">?

From Dev

How to solve the Total of my html code that linking with input type = "number"

From Dev

How to solve "Extra junk at end of file" syntax error in "01autoremove-kernels" file? // invalid characters

From Dev

'Invalid input syntax for type inet' db error in Django app with postgres and Gunicorn+Nginx as reverse proxy

Related Related

  1. 1

    Invalid input syntax for type double precision

  2. 2

    rails postgres ERROR: invalid input syntax for type double precision

  3. 3

    rails postgres ERROR: invalid input syntax for type double precision

  4. 4

    Invalid input syntax for type interval

  5. 5

    Invalid Input Syntax for Type Date

  6. 6

    How to increase precision of double type for large numbers?

  7. 7

    PostgreSQL - Error: "invalid input syntax for type bytea"

  8. 8

    "invalid input syntax for type numeric" for entering "emptyness"

  9. 9

    Postgres COPY FROM Fails With invalid input syntax for type timestamp

  10. 10

    Invalid input syntax for type date: "\N" in postgresql when fetching data

  11. 11

    pg nodejs package results in 'invalid input syntax for type json'

  12. 12

    ActiveRecord::StatementInvalid (PG::InvalidDatetimeFormat: ERROR: invalid input syntax for type date: ""

  13. 13

    Astyanax Cassandra Double type precision

  14. 14

    Astyanax Cassandra Double type precision

  15. 15

    double/float type precision in Python

  16. 16

    How to solve error: "Invalid type in JSON write (NSConcreteData)"

  17. 17

    How to solve Type mismatch issue (expected: Double, actual: Unit)

  18. 18

    How to store double with maximum precision

  19. 19

    Error: invalid input syntax for integer: ""

  20. 20

    Error: invalid input syntax for integer: ""

  21. 21

    Invalid input syntax PLPGSQL script

  22. 22

    invalid input syntax for integer (string)

  23. 23

    Create column of type double precision[] with liquibase

  24. 24

    Hive Data Type : Double Precision & Scale

  25. 25

    How to solve the Total of my html code that linking with input type = "number"

  26. 26

    How to solve 'Undefined Index' Error from <input type="radio">?

  27. 27

    How to solve the Total of my html code that linking with input type = "number"

  28. 28

    How to solve "Extra junk at end of file" syntax error in "01autoremove-kernels" file? // invalid characters

  29. 29

    'Invalid input syntax for type inet' db error in Django app with postgres and Gunicorn+Nginx as reverse proxy

HotTag

Archive