我正在处理一个项目,它有很多在python中运行的查询。当我检查土匪的时候,我看到了问题-
Test results:
>> Issue: [B608:hardcoded_sql_expressions] Possible SQL injection vector through string-based query construction.
Severity: Medium Confidence: Low
Location: main.py:160
More Info: https://bandit.readthedocs.io/en/latest/plugins/b608_hardcoded_sql_expressions.html这个问题存在于我使用原始SQL查询的所有地方。
如何修改字符串内插来解决这个问题?
样本代码-
import pandas as pd
table_name = "orders"
df = pd.read_sql(sql=f'''
SELECT * FROM {table_name};
''')我试过以下几种方法,但没有奏效。
import pandas as pd
table_name = "orders"
# Try 1
df = pd.read_sql(sql='''
SELECT * FROM {};
'''.format(table_name))
# Try 2
df = pd.read_sql(sql='''
SELECT * FROM %s;
''' %(table_name,))我遵循这个博客,但可能无法使用来自psycopg2的cursor对象。因此,需要一些可以帮助字符串格式和插值的东西,因为我将能够在pandas库中使用它。
发布于 2021-12-28 07:38:41
如果您将cur.fetchall对象转换为dataframe,那么它将工作。
cur.execute("select instrument, price, date from my_prices")
df = DataFrame(cur.fetchall(), columns=['instrument', 'price', 'date'])https://stackoverflow.com/questions/68722384
复制相似问题