0

I am trying to iterate through a dataframe and fetching values from indivdidual column to use as my parameters in sql query.

for index,frame in df1.iterrows():

      sql = "select * from issuers where column_1 = %s;"
      cur.execute(sql, frame['column_1'])
      row = cur.fetchone()
      id = row[0]
      print id

But I am getting the following error

"TypeError: not all arguments converted during string formatting"

How can I solve this? In case I need to add multiple parameters, how can I do that?

1 Answer 1

1

Instead of this:

cur.execute(sql, frame['column_1'])

Try this:

cur.execute(sql, [frame['column_1']])

The second parameter of execute is a list containing all values to be inseted into sql.

In order to insert multiple values use something as follows:

sql = "select * from issuers where column_1 = %s and column_2 = %s;"
cur.execute(sql, ["val1", "val2"])

For more information please refere to the documentation

EDIT

Here an example for INSERT INTO in SQL.

sql = "INSERT INTO user (firstname, lastname) VALUES (%s, %s)"
cur.execute(sql, ["John", "Doe"])
Sign up to request clarification or add additional context in comments.

1 Comment

@themaster I have added an example in my answer above. I hope that helps

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.