Pandas read_sql_query 参数为不带引号的字符串
Pandas read_sql_query with parameters for a string with no quotes
我想使用
将一串标识符插入一段 sql 代码中
df = pd.read_sql_query(query, self.connection,params=sql_parameter)
我的参数字典看起来像这样
sql_parameter = {'itemids':itemids_str}
其中 itemids_str 是一个类似于
的字符串
282940499, 276686324, 2665846, 46875436, 530272885, 2590230, 557021480, 282937154, 46259344
SQL 代码看起来像
SELECT
xxx,
yyy,
zzz
FROM tablexyz
where some_column_name in ( %(itemids)s )
我当前的代码使用引号插入参数
where some_column_name in ( '282940499, 276686324, 2665846, 46875436, 530272885, 2590230, 557021480, 282937154, 46259344' )
如何防止插入包含 ' 的字符串,这些不是我的字符串的一部分,但我假设它们来自使用 %s 时的参数类型字符串
我认为参数中没有为一个条件发送数值列表的规定。我总是将这样的条件直接添加到查询中
item_ids = [str(item_id) for item_id in item_ids]
where_str = ','.join(item_ids)
query = f"""SELECT
xxx,
yyy,
zzz
FROM tablexyz
where some_column_name in ({where_str})"""
我想使用
将一串标识符插入一段 sql 代码中df = pd.read_sql_query(query, self.connection,params=sql_parameter)
我的参数字典看起来像这样
sql_parameter = {'itemids':itemids_str}
其中 itemids_str 是一个类似于
的字符串282940499, 276686324, 2665846, 46875436, 530272885, 2590230, 557021480, 282937154, 46259344
SQL 代码看起来像
SELECT
xxx,
yyy,
zzz
FROM tablexyz
where some_column_name in ( %(itemids)s )
我当前的代码使用引号插入参数
where some_column_name in ( '282940499, 276686324, 2665846, 46875436, 530272885, 2590230, 557021480, 282937154, 46259344' )
如何防止插入包含 ' 的字符串,这些不是我的字符串的一部分,但我假设它们来自使用 %s 时的参数类型字符串
我认为参数中没有为一个条件发送数值列表的规定。我总是将这样的条件直接添加到查询中
item_ids = [str(item_id) for item_id in item_ids]
where_str = ','.join(item_ids)
query = f"""SELECT
xxx,
yyy,
zzz
FROM tablexyz
where some_column_name in ({where_str})"""