1

I am having issues looping a list of values that has ids to pass them as parameters into a SQL query using pyodbc connection. I read this post which seems to have lot good information, but I haven't seen an answer that loops through the list and replaces ids in each iteration.

Here is my list,

Allids = [201,202,203,204,205,206,207]

I would like to do something like this,

for i in Allids:
    cu.execute('select my_var1, my_var2, my_var2 from testdata.dbo.tbl where id = 'i'')

I get an error saying that ][SQL Server]Conversion failed when converting the varchar value 'i' to data type int.

Is it possible do this way? or convert the list into tuples and follow different methods? Any ideas would be appreciated. Thanks in advance!

cs95
  • 379,657
  • 97
  • 704
  • 746
i.n.n.m
  • 2,936
  • 7
  • 27
  • 51
  • 1
    `cu.execute('select my_var1, my_var2, my_var2 from testdata.dbo.tbl where id = \'%d\'' %i )` – cs95 Jul 14 '17 at 21:48
  • @ COLDSPEED nice, this worked, thank you. Could you give a brief explanation? Or direct me to any sources. – i.n.n.m Jul 14 '17 at 21:51
  • 1
    No, problem. Just be careful with user input data. SQL injection is real. – cs95 Jul 14 '17 at 21:53

0 Answers0