Reputation: 167
I want to insert a record with psycopg2. The code is simple:
import psycopg2
query = """
INSERT INTO xxx (lon,lat)
VALUES(%s,%s)"""
cursor.execute(query, (df2['lon'],df2['lat']))
conn.commit()
cursor.close()
conn.close()
However the code does not work and raised the below error:
psycopg2.ProgrammingError: can't adapt type 'Series'
How do I solve the issue?
Upvotes: 2
Views: 406
Reputation: 933
Seems like df2['lon']
, df2['lat']
are returning pandas Series
. We need to pass str
values in the execute function. To do this, we can use iloc to access the first value as follows:
import psycopg2
query = """
INSERT INTO xxx (lon,lat)
VALUES(%s,%s)"""
cursor.execute(query, (df2['lon'].iloc[0], df2['lat'].iloc[0])) # assumming df2 is not empty. You can add condition to check that with fallback
conn.commit()
cursor.close()
conn.close()
Upvotes: 0