Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing table name as a parameter in pyodbc

I am trying to pass a table name in pyodbc as a parameter to access data from ms sql 2005. I've tried to substitute it with ? but it never works. I would be glad to receive any advice on how to accomplish this.

like image 463
Siv Niznam Avatar asked Jul 12 '11 18:07

Siv Niznam


1 Answers

Since you are using pyodbc, I assume you are not calling a SPROC. I recommend building your SQL string in python and then passing it to SQL to execute.

import pyodbc
dbconn = pyodbc.connect(ConnectionString)
c = dbconn.cursor()

j = 'table1' #where table1 is entered, retreived, etc
query = "Select * from %s" % j
c.execute(query)
result = c.fetchall()
print 'Done'
like image 135
JMoney Avatar answered Sep 22 '22 12:09

JMoney