Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.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
Python和MySQL:传递列表/元组_Python_Mysql_Pymysql - Fatal编程技术网

Python和MySQL:传递列表/元组

Python和MySQL:传递列表/元组,python,mysql,pymysql,Python,Mysql,Pymysql,我正在用Python构建一个查询以传递给pymysql查询 条件=['m'] query=从{}中条件所在的表中选择*。formattuplecondition 我一直坚持的一点是,我想设置脚本,使其在条件可以是单个项,也可以是多个项的情况下工作 在我看来,将列表转换为元组是可行的,但事实并非如此,因为: 元组条件 返回: “m”, ,在我的mysql服务器上无法工作 最简单的设置方法是什么,我可以在python中的查询构建中将单个值或多个值发送到where子句 使用多个条件的最简单方法是为单个

我正在用Python构建一个查询以传递给pymysql查询

条件=['m'] query=从{}中条件所在的表中选择*。formattuplecondition

我一直坚持的一点是,我想设置脚本,使其在条件可以是单个项,也可以是多个项的情况下工作

在我看来,将列表转换为元组是可行的,但事实并非如此,因为: 元组条件 返回: “m”, ,在我的mysql服务器上无法工作


最简单的设置方法是什么,我可以在python中的查询构建中将单个值或多个值发送到where子句

使用多个条件的最简单方法是为单个“where”设置一个格式字符串:

fmtstr = "select * from table where condition in {} "
还有一些附加的东西:

addstr = "or condition in {} "
并根据需要连接它们

对于元组,您可以像处理列表一样处理其中的项:

x = (1, 'a')
x[0] == 1  #evaluates True
x[1] == 'a'  #same

使用多个条件的最简单方法是为单个“where”设置格式字符串:

fmtstr = "select * from table where condition in {} "
还有一些附加的东西:

addstr = "or condition in {} "
并根据需要连接它们

对于元组,您可以像处理列表一样处理其中的项:

x = (1, 'a')
x[0] == 1  #evaluates True
x[1] == 'a'  #same

您可能必须将其作为字符串传递,然后让您的sql server执行其余操作。 您是否尝试过:

query = "select * from table where condition in {}'.format(str(tuple(condition)))`

您可能必须将其作为字符串传递,然后让您的sql server执行其余操作。 您是否尝试过:

query = "select * from table where condition in {}'.format(str(tuple(condition)))`

我相信这会解决你的问题:

condition=['m', 'n']

def quoteWrap(path):
    return '"' + path + '"'

query = "select * from table where condition in ({})".format(','.join([quoteWrap(c) for c in condition]))
query
#select * from table where condition in ("m","n")

我还添加了quoteWrap函数,显然,可以将字符串用引号括起来。

我相信这应该可以解决您的问题:

condition=['m', 'n']

def quoteWrap(path):
    return '"' + path + '"'

query = "select * from table where condition in ({})".format(','.join([quoteWrap(c) for c in condition]))
query
#select * from table where condition in ("m","n")

我还添加了quoteWrap函数,显然,可以将字符串用引号括起来。

我能想到的另一个技巧是替换查询的最后一部分

如果只有一个元素,则通常会出现问题,即在末尾添加一个不必要的逗号,如“m”

为什么不这样做:

condition = ['m']
queryString = 'SELECT o_id FROM orders WHERE o_k_id IN ' + str(tuple(condition))
queryString = queryString.replace(',)', ')')
print(queryString)
因此,您的查询将如下所示:

select * from table where condition in ('m')
如果您必须将多个值传递给where条件,则该条件仍然有效:

condition = ['m', 'n']
queryString = 'select * from table where condition in ' + str(tuple(condition))
queryString = queryString.replace(',)', ')')
print(queryString)
输出:

select * from table where condition in ('m', 'n')

我能想到的另一个技巧是替换查询的最后一部分

如果只有一个元素,则通常会出现问题,即在末尾添加一个不必要的逗号,如“m”

为什么不这样做:

condition = ['m']
queryString = 'SELECT o_id FROM orders WHERE o_k_id IN ' + str(tuple(condition))
queryString = queryString.replace(',)', ')')
print(queryString)
因此,您的查询将如下所示:

select * from table where condition in ('m')
如果您必须将多个值传递给where条件,则该条件仍然有效:

condition = ['m', 'n']
queryString = 'select * from table where condition in ' + str(tuple(condition))
queryString = queryString.replace(',)', ')')
print(queryString)
输出:

select * from table where condition in ('m', 'n')

所以我选择了另一条路线,因为这些建议要么太麻烦,要么根本不起作用

对我有效的解决方案是: cond=','。连接'{0}'。在条件中为w格式化w

然后查询是: 从{}.formatcond中条件所在的表中选择*`

这将生成一个逗号分隔的值字符串,每个值都用引号括起来。例如:

条件=['baseline','error'] cond=','。连接'{0}'。在条件中为w格式化w 基线,误差 query=从{}`.formatcond中条件所在的表中选择* 从基线中条件所在的表中选择*错误
所以我选择了另一条路线,因为这些建议要么太麻烦,要么根本不起作用

对我有效的解决方案是: cond=','。连接'{0}'。在条件中为w格式化w

然后查询是: 从{}.formatcond中条件所在的表中选择*`

这将生成一个逗号分隔的值字符串,每个值都用引号括起来。例如:

条件=['baseline','error'] cond=','。连接'{0}'。在条件中为w格式化w 基线,误差 query=从{}`.formatcond中条件所在的表中选择* 从基线中条件所在的表中选择*错误
一旦应用.format,查询将是一个字符串。我认为转换不会改变任何东西。一旦应用.format,查询将是一个字符串。我不认为转换改变了什么。嘿,这接近于我最终得到的结果——没有写函数,但它直接看到了我的答案。这是迄今为止最好、最灵活的解决方案。嘿,这接近于我最终得到的结果——没有编写函数,但它直接看到了我的答案。这是迄今为止最好、最灵活的解决方案。