1

I know this question have been asked before but any of the answers was helpful for me, I am using Basic module(psycopg2) in python trying to pass an external parameter row[1] into a Postgres Query which is:

 print row[1], type(row[1]) 

630776863141535744 <type 'str'>

when I run the following code I face TypeError: not all arguments converted during string formatting

for row in rows:
   t=row[1]
   PSQL1="SELECT occorrenza.tag from occorrenza where tweet=%s and tag in ( select pk_tag from hashtag where isentity=FALSE);"
   c.execute(PSQL1,t)

also I have tried the following code and have the same error :

for row in rows:
    c.execute("SELECT occorrenza.tag from occorrenza where tweet=%s and tag in ( select pk_tag from hashtag where isentity=FALSE);",row[1])

I have changed many other parts like putting '%s'instead of %s but I still have error!

1 Answer 1

2

The second parameter to execute() should be a tuple in your case:

for row in rows:
    c.execute("SELECT occorrenza.tag from occorrenza where tweet=%s and tag in ( select pk_tag from hashtag where isentity=FALSE);", (row[1], ))
Sign up to request clarification or add additional context in comments.

2 Comments

Also, query should be parametrised with ?, not with %s as in string firmatting.
@Rogalski nope, %s is correct, proof: initd.org/psycopg/docs/…. Thanks.

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.