pyodbc-如何使用变量作为参数来执行选择语句 [英] pyodbc - How to perform a select statement using a variable for a parameter

查看:323
本文介绍了pyodbc-如何使用变量作为参数来执行选择语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历名为吞吐量"的表中的所有行,但要遍历特定的DeviceName(已存储在data ['DeviceName']中.我尝试了以下操作,但不起作用:

I'm trying to iterate through all the rows in a table named Throughput, but for a specific DeviceName (which I have stored in data['DeviceName']. I've tried the following, but it doesn't work:

for row in cursor.execute("select * from Throughput where DeviceName=%s"), %(data['DeviceName']):

也尝试过此操作,但不起作用:

also tried this but it doesn't work:

for row in cursor.execute("select * from Throughput where(DeviceName), values(?)", (data['DeviceName']) ):

我的最终工作代码段:

A snippet of my final working code:

query = "select * from Throughput where DeviceName = '%s'" % data['Device Name']
      try:
          for row in cursor.execute(query):

推荐答案

在不知道DeviceName列的类型和哪个数据库服务器的情况下,我会引用用于约束DeviceName的字符串

Without knowing the type of the column DeviceName and what database server, I would quote the string that is being used to constrain DeviceName

"select * from Throughput where DeviceName='%s'" % data['DeviceName']

看看会发生什么.

这篇关于pyodbc-如何使用变量作为参数来执行选择语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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