psycopg2 E'在表、字段和模式上
我遇到了同样的问题: python 将“E”添加到字符串
给出的所有答案都是相关的,但我正在打破我的脖子就在这上面。
问题是 psycopg2 不仅转义值,还转义模式、表和列名称,如下所示:
CREATE TABLE E'Tablename' (E'identificatie' VARCHAR(16))
它根本不应该这样做!我如何摆脱表名和列的 E 和 '' 但保留字段值的 E 和 '' ?
另一种选择
'CREATE TABLE ' + tablename + ' (' + fieldname... %
使其再次容易受到 SQL 注入的攻击。
陷入进退两难的境地..
I am having the same problem as:
python adds "E" to string
All the answers given are relevant, but I am breaking my neck on this one.
The problem is that psycopg2 not only escapes values, but also schema, table and column names like this:
CREATE TABLE E'Tablename' (E'identificatie' VARCHAR(16))
Which it simply shouldn't! How van I get rid of the E and '' for table names and columns but maintain them for field values?
the alternative
'CREATE TABLE ' + tablename + ' (' + fieldname... %
makes it vulnerable to sql injection all over again.
Stuck between a rock and a hard place..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论好坏,Python 接口和 Psycopg 通常不支持将用户提供的标识符替换为 SQL 命令。你必须自己动手。只需几行代码即可完成。
It is, for better or worse, generally not supported by the Python interfaces and Psycopg in particular to substitute user-supplied identifiers into SQL commands. You will have to roll your own. It can be done with a few lines of code.
好的,谢谢彼得,至少我知道不要再看了。我决定采取不同的方法:
使用脚本文件生成数据库,而不是从代码生成数据库。这将使对数据库进行“版本控制”变得更加容易。
同时,我正在查看 sqlalchemy http://www.sqlalchemy.org/ 它几乎做了什么我想要,但目前还只是一步,因为它需要对我正在重建的应用程序进行彻底的重组
Ok, thanks Peter, at least I know not to look any further. I decided to take a different approach:
Use a script file to generate the database instead of generating it from code. This will make it more easy to have "versioning" on the database.
Meanwhile, I am taking a look at sqlalchemy http://www.sqlalchemy.org/ which pretty much does what I want but is currently a step to far as it requires a drastic restructure of the application I am rebuilding