I have an Issue with updating SQL columns with lists in Python.
I have list list_arrive and I can't update my values in arrive column in PostgresSQL. Included list as an example
import pandas as pd
import psycopg2
import numpy as np
# Getting values from Location.csv and converting it into DF Note:(I have Multiple Empty Rows and column arrive)
df = pd.read_csv("Locations.csv")
connection = psycopg2.connect(user="user",
password="password",
host="localhost",
port="5432",)
cursor = connection.cursor()
query = "SELECT * FROM location"
cursor.execute(query)
# I tried To Convert Df column type to str due to(TypeError: not all arguments converted during string formatting)Error
df['arrive'] = df['arrive'].astype(str)
# Replaced All Empty Rows With "NaN" Values
df['arrive'] = df['arrive'].replace(r'^\s*$', np.NaN, regex=True)
# Replaced "NaN" Values With None String
df['arrive'].fillna("None",inplace= True)
# Created List
list_arrive = list(df['arrive'])
sql_query = "UPDATE location SET arrive = %s"
# I tried:
cursor.execute(sql_update, (list_arrive,))
cursor.execute(sql_update, list_arrive)
# Tried with executemany
# Tried to Loop trough list_arrive
connection.commit()
list_arrive= ['Cranbury, NJ', 'None', 'New Orleans, LA', 'Ashland,VA', 'Lancaster, NY ', 'None', 'Hazelwood,MO', 'Aurora, CO', 'None']
Note that I'm updating only arrive column values in SQL database which has already other columns and values
I'm having three types of problem:
- Getting "TypeError: not all arguments converted during string formatting"
- When I tried to just
execute()it pasted first value over and over with{}brackets - If I use
forloop, it loops with just first values.
UPDATEhas noWHEREclause so all the rows in the table will get the same value forarrive. Iterate over the list and pass value to%sas(list_value,), again you need to specify which row toUPDATEwith aWHERE'. Otherwise thearrive` will be the last one passed and it will be the same for all rows. FYI, I'm pretty sure this;import numoy as npis supposed to be;import numpy as npexecutemanycall which should be work to pass multiple values to same parameter. Even show yourforloop version. Don't just comment you tried.cursor.executemany(sql_update,list_arrive)With Loop And execute many gives the same result @Parfaitexecutemanyrepeats only the first value! Anyway as commented above, without aWHEREclause you repeatedly update all rows, so the very last item in list is retained for field.