Python Forum
Insert using psycopg giving syntax error near "INSERT INTO"
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert using psycopg giving syntax error near "INSERT INTO"
#1
Hi there all

I am using the psycopg2 module for postgresql and I keep receiving an error in my SQL statement saying there is a syntax error at or near "INSERT INTO. I've stared at it forever and I just can't seem to find it, maybe someone can help me? Thanks!

The connection is working fine between the database, and dates is a list of dates also autocommit is set to True

def addToDatabase(self, dates):
.
.
.
   sql = """INSERT INTO "%s" ("Date") VALUES(%s);"""

       for date in range(0, len(dates)):
           data = (ticker, dates[date])
           cursor.execute(sql, data)
.
.
.
Reply
#2
Please:
  • Show the code in context
  • Show actual (verbatim) error traceback
Thank you
Reply
#3
The error traceback is as follows:
Traceback (most recent call last):
  File "stockMain.py", line 21, in <module>
    main()
  File "stockMain.py", line 8, in main
    databases.addToDatabases(databases.dataFeed.genDates['daily'])
  File "/home/dondusko/Documents/AtomProjects/StockProject/stockDatabase.py", line 110, in addToDatabases
    cursor.execute(sql, data)
psycopg2.ProgrammingError: syntax error at or near "INSERT INTO"
LINE 1: INSERT INTO "'AAPL'" ("Date") VALUES('2008-01-01');

This is the function in question, not exactly sure what you mean code in context so this might be helpful?:
def addToDatabases(self, dates):
       ticker = self.getTickerName()

       for databaseName in range(0, len(self.databaseNames)):
           tempConnection = psycopg2.connect(dbname="'"+self.databaseNames[databaseName]+"'", user=self.databaseCredentials['username'], host=self.databaseCredentials['host'], password=self.databaseCredentials['password'])
           cursor = tempConnection.cursor()
           tempConnection.autocommit = True

           sql = """CREATE TABLE IF NOT EXISTS "%s" (""" + self.getTableSQL() + ");"
           data = (ticker, )
           cursor.execute(sql, data)

           sql = """INSERT INTO "%s" ("Date") VALUES(%s);"""

           for date in range(0, len(dates)):
               data = (ticker, dates[date])
               cursor.execute(sql, data)

           cursor.close()
           tempConnection.close()
Reply
#4
add a print statement after line 9
print(sql: {}'.format(sql))
Reply
#5
(Jul-18-2017, 03:58 AM)olgethorpe Wrote: INSERT INTO "'AAPL'"
I don't know postgres, but for most databases, it's an sql error to quote a table name.  If you can't just use the table name (ie: insert into AAPL), you could use brackets (ie: insert into [AAPL]), or sometimes backticks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Insert command line in script lif 4 1,033 Mar-24-2025, 10:30 PM
Last Post: lif
  BST insert using recursive hichipi12 5 4,631 Jan-26-2025, 02:30 AM
Last Post: bterwijn
  How to insert text time - into video frames? oxidian 0 1,337 Aug-25-2024, 04:51 PM
Last Post: oxidian
  POST Syntax error amplay 0 667 Aug-07-2024, 02:43 PM
Last Post: amplay
  The INSERT permission was denied on the object Steven5055 3 3,024 Jun-12-2024, 08:13 AM
Last Post: GregoryConley
  is this really a syntax error? Skaperen 4 1,673 May-25-2024, 07:31 AM
Last Post: snippsat
  World Clock syntax error OscarBoots 1 1,140 May-03-2024, 05:20 AM
Last Post: snippsat
  Syntax error for "root = Tk()" dlwaddel 15 6,782 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 1,360 Jan-19-2024, 01:20 PM
Last Post: rob101
  How to insert Dashed Lines in between Rows of a tabulate output Mudassir1987 0 1,182 Sep-27-2023, 10:09 AM
Last Post: Mudassir1987

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020