How can we update the multiple ids which are in csv file in python script?
Question:
I am trying to update the table based on id. I have multiple ids more than 1.4 million. So I have kept the 1.4 million ids in one CSV file and in my code using chunks. As of now I have kept 10 records in my CSV file for testing. But my update query is not working. in the id values passing like id = ‘3 a6P3a0000058StwEAE.
import pandas as pd
from sqlalchemy import create_engine
DF = pd.read_csv("/home/bha/harddeleteid10.csv", names=['colm_id'], header=None)
def chunker(seq, size):
return (seq [pos:pos + size] for pos in range (0, len(seq), size))
for i in chunker(DF['colm_id'],3):
engine = create_engine("db", echo=True)
conn = engine.connect()
conn.autocommit = True
conn.execute(f"""update salesforce_fr.apttus_proposal__proposal_line_item__c_poc set isdeleted=True where id = '{i}'""")
Below is id passing in query from the above loop(i is in series datatype).
update salesforce_fr.apttus_proposal__proposal_line_item__c_poc set isdeleted=True where id = '6 a6P3a0000057TGuEAM
Answers:
The main problem is with the textual SQLAlchemy statement. It does not follow the sqlalchemy textual SQL recommendations.
Replace this
conn.execute(f"""update salesforce_fr.apttus_proposal__proposal_line_item__c_poc set isdeleted=True where id = '{i}'""")
With this
# build a sqlalchemy statement using textual SQL
statement = text("update salesforce_fr.apttus_proposal__proposal_line_item__c_poc set isdeleted=True where id = (:x)")
statement.bindparams(x=i)
conn.execute(statement)
There was a lot of extra code in your example. I hope this helps.
I am trying to update the table based on id. I have multiple ids more than 1.4 million. So I have kept the 1.4 million ids in one CSV file and in my code using chunks. As of now I have kept 10 records in my CSV file for testing. But my update query is not working. in the id values passing like id = ‘3 a6P3a0000058StwEAE.
import pandas as pd
from sqlalchemy import create_engine
DF = pd.read_csv("/home/bha/harddeleteid10.csv", names=['colm_id'], header=None)
def chunker(seq, size):
return (seq [pos:pos + size] for pos in range (0, len(seq), size))
for i in chunker(DF['colm_id'],3):
engine = create_engine("db", echo=True)
conn = engine.connect()
conn.autocommit = True
conn.execute(f"""update salesforce_fr.apttus_proposal__proposal_line_item__c_poc set isdeleted=True where id = '{i}'""")
Below is id passing in query from the above loop(i is in series datatype).
update salesforce_fr.apttus_proposal__proposal_line_item__c_poc set isdeleted=True where id = '6 a6P3a0000057TGuEAM
The main problem is with the textual SQLAlchemy statement. It does not follow the sqlalchemy textual SQL recommendations.
Replace this
conn.execute(f"""update salesforce_fr.apttus_proposal__proposal_line_item__c_poc set isdeleted=True where id = '{i}'""")
With this
# build a sqlalchemy statement using textual SQL
statement = text("update salesforce_fr.apttus_proposal__proposal_line_item__c_poc set isdeleted=True where id = (:x)")
statement.bindparams(x=i)
conn.execute(statement)
There was a lot of extra code in your example. I hope this helps.