在pyodbc中将表名作为参数传递 [英] Passing table name as a parameter in pyodbc

查看:142
本文介绍了在pyodbc中将表名作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在pyodbc中传递表名作为参数,以访问ms sql 2005中的数据.我试图用?代替它,但它永远行不通.我很高兴收到关于如何完成此操作的任何建议.

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.

推荐答案

由于您使用的是pyodbc,因此我假设您未调用SPROC. 我建议使用python构建您的SQL字符串,然后将其传递给SQL以执行.

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'

这篇关于在pyodbc中将表名作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆