0

hi i am korean so not native english speaker

anyway i have question

when i learning sql and python flask connect

this is my code

  sql='''select count(*) from member where id = :1 '''
    result=curs.execute(sql,(join_id))
    cnt=result.fetchone()

this code print (ORA-01036: illegal variable name/number) error message

ok i solved error

 sql='''select count(*) from member where id = :1 '''
    result=curs.execute(sql,(join_id,)
    cnt=result.fetchone()

like this( join_id (add),)

that is my question

why different result (,) or not

i think it just one binding variable so why need (,)??

MT0
  • 143,790
  • 11
  • 59
  • 117
ewise
  • 1
  • 1
  • 1
    `(join_id)` is not a tuple it is just parenthesis around your variable and can be simplified to `join_id`. `(join_id,)` is a tuple. – MT0 Sep 26 '22 at 10:49

1 Answers1

0

Because the binding variables always need to be in a tuple, even if it's a 1-tuple. (This stands for all Python SQL libraries that adhere to the DB-API spec.)

Since in Python (foo) is just a parenthesized foo, to build an 1-tuple, you'll need the trailing comma (see e.g. How to create a tuple with only one element).

AKX
  • 152,115
  • 15
  • 115
  • 172