【问题标题】:How to use integer variable in sql query如何在sql查询中使用整数变量
【发布时间】:2021-02-15 17:50:10
【问题描述】:

使用 python 3 我正在使用以下查询搜索数据库,但是,num_tests 是在我的 python 代码中计算的一个不断变化的整数值。其中:

num_tests = randint(1, 100)

如何在不将其更新到数据库的情况下搜索值 num_tests,我希望 sql 查询中的 num_tests 简单地采用 num_tests 的预定值

test_db.execute('select count(testers) from test where' number_of_testers = num_tests')

【问题讨论】:

  • 你在寻找字符串插值吗?

标签: python mysql python-3.x sqlite


【解决方案1】:

在 sql 语句中使用 placeholders,然后传入一个包含您要查找的特定值的元组:

test_db.execute('select count(testers) from test where number_of_testers = ?', (num_tests,))

【讨论】:

    【解决方案2】:

    你可以用这个:

    test_db.execute('select count(testers) from test where number_of_testers = ?', num_tests)
    

    【讨论】:

      猜你喜欢
      • 2021-03-09
      • 2021-09-05
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      相关资源
      最近更新 更多