I am trying to do a script in python that will fetch data from table one and input into another table. Sort of like an ETL of some sorts.
I am however running into this SyntaxError: unexpected EOF while parsing
error.
I am sort of harzading my way around and trying to use techniques that I have seen others use so I don't really know my way around much.
Here's my code so far:
import psycopg2
try:
connectionone = psycopg2.connect(user = "postgres",
password = "xxxxxx",
host = "127.0.0.1",
port = "5432",
database = "xxxxxx")
connectiontwo = psycopg2.connect(user = "postgres",
password = "xxxxxx",
host = "127.0.0.1",
port = "5432",
database = "xxxxxx")
cursorsource = connectionone.cursor()
cursordest = connectiontwo.cursor()
#Truncating dest table
print("Truncating Destination")
cursordest.execute('delete from testarea.salepersons_2')
connectiontwo.commit()
#Fetch source data
cursorsource.execute('SELECT sp_no, sp_name, sp_territory, sp_product,
active FROM testarea.salepersons_original;')
rows = cursorsource.fetchall()
sql_insert = 'INSERT INTO testarea.salepersons_2 (sp_no, sp_name,
p_territory, sp_product, active) values '
sql_values = ['(%s, %s, %s, %s, %s)']
data_values = []
batch_size = 1000 #customize for size of tables...
sql_stmt = sql_insert + ','.join(sql_values*batch_size) + ';'
for i, row in enumerate(rows, 1):
data_values += row[:5] #relates to number of columns (%s)
if i % batch_size == 0:
cursordest.execute (sql_stmt , data_values )
cursordest.commit()
print("Inserting")
data_values = []
if (i % batch_size != 0):
sql_stmt = sql_insert + ','.join(sql_values*(i % batch_size)) +
';'
cursordest.execute (sql_stmt, data_values)
print("Last Values ....")
connectiontwo.commit()
except (Exception, psycopg2.Error) as error :
print ("Error occured :-(", error)
finally:
#closing database connection.
if(connectionone):
cursorsource.close()
connectionone.close()
print("PostgreSQL connection is closed")
#closing database connection.
if(connectiontwo):
cursordest.close()
connectiontwo.close()
print("PostgreSQL connection is closed")
#close connections
cursorsource.close()
cursordest.close()
cursorsource.close()
cursordest.close()