【问题标题】:Is there a way to dynamically enter the values for SQL query without value placeholders有没有办法在没有值占位符的情况下动态输入 SQL 查询的值
【发布时间】:2019-12-02 14:59:23
【问题描述】:

假设我在 Python 中有 sqlite3 查询:

SELECT COUNT(customerid) FROM customerDetails WHERE age BETWEEN 17 AND 24

是否有通过用户输入动态输入年龄占位符的方法。

这可以通过 sql 变量来完成吗?它会像这样吗?..

SELECT COUNT(customerid) FROM customerDetails WHERE age BETWEEN = ? AND = ?

【问题讨论】:

    标签: sql python-3.x sqlite


    【解决方案1】:

    是的。只需使用参数化查询并将占位符 ?s 的值作为元组传递给 execute 方法:

    min = 10
    max = 50
    sql = "SELECT COUNT(customerid) FROM customerDetails WHERE age BETWEEN = ? AND = ?"
    cursor.execute(sql, (min, max))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-05
      • 2021-08-17
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多