首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python变量

Python变量
EN

Stack Overflow用户
提问于 2018-06-05 13:54:53
回答 1查看 51关注 0票数 0
代码语言:javascript
复制
def copy_sql_db(storage_connection, mysql_connection, date):
    cursor = storage_connection.cursor()
    mysql_cursor = mysql_connection.cursor()

    if date == "ALL":
      cursor.execute("""select name, test_time, id from test_table""")
    else:
      cursor.execute("""select name, test_time, id from test_table where test_time > (CURDATE() - INTERVAL date day)""")
    ...

我不知道要搜索什么才能将变量 date 传递给我的函数copy_sql_db到there :运行创建的脚本的cursor.execute...when在test_time >date附近的SQL语法中有一个错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-05 13:58:29

您需要使用占位符

试试这个:

代码语言:javascript
复制
    cursor.execute("""select name, test_time, id from test_table where test_time > (CURDATE() - INTERVAL {0} day)""".format(date))

安全

代码语言:javascript
复制
    cursor.execute("""select name, test_time, id from test_table where test_time > (CURDATE() - INTERVAL %s day)""", (date,))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50701902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档