0

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:

  1. Getting "TypeError: not all arguments converted during string formatting"
  2. When I tried to just execute() it pasted first value over and over with {} brackets
  3. If I use for loop, it loops with just first values.
6
  • 3
    Your UPDATE has no WHERE clause so all the rows in the table will get the same value for arrive. Iterate over the list and pass value to %s as (list_value,), again you need to specify which row to UPDATE with a WHERE'. Otherwise the arrive` will be the last one passed and it will be the same for all rows. FYI, I'm pretty sure this; import numoy as np is supposed to be; import numpy as np Commented Jan 19, 2021 at 20:58
  • 1
    Please show your executemany call which should be work to pass multiple values to same parameter. Even show your for loop version. Don't just comment you tried. Commented Jan 19, 2021 at 21:11
  • cursor.executemany(sql_update,list_arrive) With Loop And execute many gives the same result @Parfait Commented Jan 19, 2021 at 21:29
  • stackoverflow.com/questions/20699196/… i even tried this one but not working somehow in my case @Parfait Commented Jan 19, 2021 at 21:32
  • 1
    I cannot see how executemany repeats only the first value! Anyway as commented above, without a WHERE clause you repeatedly update all rows, so the very last item in list is retained for field. Commented Jan 19, 2021 at 22:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.