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
  Syntax error for "root = Tk()" dlwaddel 15 1,006 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 333 Jan-19-2024, 01:20 PM
Last Post: rob101
  How to insert Dashed Lines in between Rows of a tabulate output Mudassir1987 0 463 Sep-27-2023, 10:09 AM
Last Post: Mudassir1987
  sqlite3 Conn Insert Value Error TylerDunbar 3 674 Sep-04-2023, 06:32 PM
Last Post: deanhystad
  Syntax error while executing the Python code in Linux DivAsh 8 1,450 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  docx insert words of previuos paragraph on next paragraph in the same position ctrldan 7 1,168 Jun-20-2023, 10:26 PM
Last Post: Pedroski55
  Code is returning the incorrect values. syntax error 007sonic 6 1,134 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  openpyxl insert picture problem cools0607 2 1,390 May-03-2023, 06:48 AM
Last Post: cools0607
  Insert 10gb csv files into sql table via python mg24 2 1,831 Apr-28-2023, 04:14 PM
Last Post: snippsat
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,468 Mar-27-2023, 07:38 AM
Last Post: buran

Forum Jump:

User Panel Messages

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