0

I am simply trying to insert a new row into an existing Postgres database with the Python Psycopg2 module's executeValues function. I am using this because in the future I will need to do a batch insert, but for now I am only testing one row.

Here is the code:

import psycopg2
from psycopg2.extras import *

 # Connect to database

try:
    conn = psycopg2.connect("dbname='test' user='postgres' host='localhost' password='serengeti'")
    conn.autocommit = True
except:
    print "I am unable to connect to the database"

cur = conn.cursor()
SQL = "INSERT INTO sample (id,name) VALUES %s" 
valuesList = []

valuesList.append((5,'wolf')) 

**The problem appears to be here somewhere**
valuesList.append(('(SELECT id FROM city WHERE name =''Boston'')','tiger'))

execute_values(cur,SQL,valuesList,template=None, page_size=1000000)

The error I am getting is shown in this image:

error

'Sample' is the table to be inserted into. It contains two fields: 'id' (integer) and 'name' (text). 'City' is another table in the same database that contains an 'id'(integer) and 'name' field (text) with a row where name = 'Boston' that has already been populated.

I believe the error is somewhere in the SELECT statement inside the valuesList.

Can anyone assist?

Thanks!

0 Answers0