Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在where子句中使用字符串比较的Python MySQL查询_Python_Mysql - Fatal编程技术网

在where子句中使用字符串比较的Python MySQL查询

在where子句中使用字符串比较的Python MySQL查询,python,mysql,Python,Mysql,我遇到了一个奇怪的问题,我无法获得一个带有参数的SQL查询来处理where子句中的字符串比较——我没有返回一行。当我通过bash连接到MySQL数据库时,查询工作正常 python 3.7.3 mysql连接器python==8.0.11 mysql 5.7 工作(获取我的行): 也有效(获取我的行): 不工作(cursor.fetchall()是[]): 删除引号: cursor.execute( """ select * from my_table

我遇到了一个奇怪的问题,我无法获得一个带有参数的SQL查询来处理where子句中的字符串比较——我没有返回一行。当我通过bash连接到MySQL数据库时,查询工作正常

  • python 3.7.3
  • mysql连接器python==8.0.11
  • mysql 5.7
工作(获取我的行):

也有效(获取我的行):

不工作(
cursor.fetchall()
[]
):

删除引号:

cursor.execute(
    """
        select *
        from my_table
        where my_column = %s
    """,
    ('my_string')
)

小心元组。我想你需要
('my_string',)



仅供参考,@tscherg在问题下方的评论中提到了原始评论。

不起作用
的确切含义是什么?您是否收到错误?有一条注释已被删除-在字符串后用逗号书写('my_string')。请添加为答案,以便我可以将其推广为正确答案!不是这样。得到一个MySQL错误(42000),告诉我检查%s附近的语法,同时删除了%s周围的引号!
cursor.execute(
    """
        select *
        from my_table
        where my_column = 'my_string'
    """
)
cursor.execute(
    """
        select *
        from my_table
        where my_column = '%s'
    """,
    ('my_string')
)
cursor.execute(
    """
        select *
        from my_table
        where my_column = %s
    """,
    ('my_string')
)