python - 如何使用用户指定的列构建 SQL Select 语句?
问题描述
我想按用户指定的名称选择列,但我想避免 SQL 注入。
我认为这样的事情不是一个好主意:
column_one = input("Column one")
column_two = input("Column two")
column_three = input("Column three")
sql.execute("Select {}, {}, {} from table;".format(column_one, column_two, column_three))
我可以使用类似的东西:
con = sql.connect("example.sqlite3")
select = "select ? from table;"
t = ('id', )
cur = con.cursor()
cur.execute(selec, t)
rows = cur.fetchall()
con.commit()
con.close()
for row in rows:
for cell in row:
print(cell)
我得到的是: id id id id id id id ...
解决方案
编写查询以返回表中的所有列,然后使用条件逻辑选择要返回的列。
推荐阅读
- python - 用 Python 写 binp 文件,用 Matlab 读取
- r - 有什么方法可以像 Python 一样为 R 代码构建 Heroku
- java - Vaadin DropDown/Select/Spinner 最佳实践?
- tensorflow - Colab 没有名为“tensorflow.examples.tutorials”的模块
- angular - 如何使用 Angular 更改对象值
- node.js - Nestjs passport-facebook 无效的 OAuth 访问令牌
- java - 稍后如何在这里初始化final?
- flutter - 使用返回小部件的功能在按钮单击时添加新小部件
- mysql - 如何将表列转换为mysql中的表?
- asp.net - 使用 asp.net core 和 react js 刷新时项目为空白